<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>云声の博客</title>
  
  <subtitle>云声的小白之路</subtitle>
  <link href="https://www.kiilin.com/atom.xm" rel="self"/>
  
  <link href="https://www.kiilin.com/"/>
  <updated>2020-12-31T02:06:41.546Z</updated>
  <id>https://www.kiilin.com/</id>
  
  <author>
    <name>云声</name>
    
  </author>
  
  <generator uri="https://hexo.io/">Hexo</generator>
  
  <entry>
    <title>docker学习笔记</title>
    <link href="https://www.kiilin.com/blog/docker.html"/>
    <id>https://www.kiilin.com/blog/docker.html</id>
    <published>2019-06-27T08:53:00.000Z</published>
    <updated>2020-12-31T02:06:41.546Z</updated>
    
    <content type="html"><![CDATA[<h2 id="一、Docker简介"><a href="#一、Docker简介" class="headerlink" title="一、Docker简介"></a>一、Docker简介</h2><h3 id="1-Docker是什么？"><a href="#1-Docker是什么？" class="headerlink" title="1. Docker是什么？"></a>1. Docker是什么？</h3><p>​    产生背景：</p><ul><li>开发和运维之间因为环境不同而导致的矛盾（不同的操作系统、软件环境、应用配置等） DevOps</li><li>集群环境下每台服务器都配置相同的环境，太麻烦</li><li>解决“在我的机器上可以正常工作”的问题</li></ul><p>​        Docker是一个开源的应用容器引擎，让开发者可以打包他们的应用以及依赖包到一个可移植的容器中，然后发布到任何流行的 Linux 机器上，也可以实现虚拟化。</p><a id="more"></a><p>​    阿里云、百度云等都支持Docker技术</p><p>​    官网：<span class="exturl" data-url="aHR0cHM6Ly93d3cuZG9ja2VyLmNvbS8=">https://www.docker.com/<i class="fa fa-external-link-alt"></i></span></p><p>​    中文官网：<span class="exturl" data-url="aHR0cDovL3d3dy5kb2NrZXItY24uY29tLw==">http://www.docker-cn.com/<i class="fa fa-external-link-alt"></i></span></p><h3 id="2-Docker作用"><a href="#2-Docker作用" class="headerlink" title="2. Docker作用"></a>2. Docker作用</h3><p>​    Docker是一种容器技术，使用Docker可以：</p><ul><li>将软件环境安装并配置好，打包成一个镜像Image，然后将该镜像发布出去（Docker仓库）</li><li>其他使用者可以在仓库中下载获取这个镜像</li><li>通过Docker运行这个镜像，就可以获取同样的环境（容器）</li></ul><p>​        Docker简化了环境部署和配置，实现“一次构建，处处运行”，避免了因运行环境不一致而导致的异常</p><p>​         可以将Docker简单的认为是一个虚拟机，可以运行各种软件环境的虚拟机，但与传统虚拟机技术有所不同</p><p>​    Docker容器技术与传统虚拟机技术的区别：</p><ul><li><p>传统虚拟机技术：模拟一个完整的操作系统，先虚拟出一套硬件，然后在其上安装操作系统，最后在系统上再运行应用程序</p><p>缺点：资源占用多，启动慢</p></li><li><p>Docker容器技术：不是模拟一个完整的操作系统，没有进行硬件虚拟，而是对进程进行隔离，封装成容器，容器内的应用程序是直接使用宿主机的内核，且容器之间是互相隔离的，互不影响</p><p>优点：更轻便、效率高、启动快、秒级</p></li></ul><h3 id="3-基本术语"><a href="#3-基本术语" class="headerlink" title="3. 基本术语"></a>3. 基本术语</h3><p>​    术语：</p><ul><li><p>Docker主机（Host）</p><p>安装了Docker程序的主机，运行Docker守护进程</p></li><li><p>Docker镜像（Image）</p><p>将软件环境打包好的模板，用来创建容器的，一个镜像可以创建多个容器</p></li><li><p>Docker容器（Container）</p><p>运行镜像后生成的实例称为容器，每运行一次镜像就会产生一个容器，容器可以启动、停止或删除</p><p>容器使用是沙箱机制，互相隔离，是独立是安全的</p><p>可以把容器看作是一个简易版的Linux环境，包括用户权限、文件系统和运行的应用等</p></li><li><p>Docker仓库（Repository）</p><p>用来保存镜像的，仓库中包含许多镜像，每个镜像都有不同的标签Tag</p><p>官方仓库：<span class="exturl" data-url="aHR0cHM6Ly9odWIuZG9ja2VyLmNvbS8=">https://hub.docker.com/<i class="fa fa-external-link-alt"></i></span></p></li></ul><p>​        使用Docker的步骤：</p><ol><li>安装Docker</li><li>从Docker仓库中下载软件对应的镜像</li><li>运行这个镜像，此时会生成一个Docker容器</li><li>对容器的启动/停止就是对软件的启动/停止</li></ol><h2 id="二、准备Linux系统"><a href="#二、准备Linux系统" class="headerlink" title="二、准备Linux系统"></a>二、准备Linux系统</h2><h3 id="1-安装虚拟机软件"><a href="#1-安装虚拟机软件" class="headerlink" title="1. 安装虚拟机软件"></a>1. 安装虚拟机软件</h3><p>​    VMware、VirtualBox</p><p>​    Ubuntu、RHEL、CentOS、Debian、SLES（SUSE）</p><h3 id="2-新建虚拟机"><a href="#2-新建虚拟机" class="headerlink" title="2. 新建虚拟机"></a>2. 新建虚拟机</h3><p>​    版本：Red Hat（64-bit）</p><p>​    网络：桥接网卡</p><p>​    光驱：选择操作系统的iso文件</p><h3 id="3-安装CentOS"><a href="#3-安装CentOS" class="headerlink" title="3. 安装CentOS"></a>3. 安装CentOS</h3><h3 id="4-连接Linux服务器"><a href="#4-连接Linux服务器" class="headerlink" title="4. 连接Linux服务器"></a>4. 连接Linux服务器</h3><p>​    步骤：</p><ol><li><p>查看服务器ip地址、</p><p>在虚拟机中执行<code>ip addr</code></p></li><li><p>连接服务器</p><p>在客户端中执行<code>ssh 服务器账户@服务器地址</code>，然后输入密码</p><p>注：SSH是Secure Shell的缩写，用于远程登陆访问的协议</p></li></ol><h3 id="5-基本操作"><a href="#5-基本操作" class="headerlink" title="5. 基本操作"></a>5. 基本操作</h3><h4 id="5-1-常用命令"><a href="#5-1-常用命令" class="headerlink" title="5.1 常用命令"></a>5.1 常用命令</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cat /proc/cpuinfo  <span class="comment"># 查看cpu信息</span></span><br><span class="line">cat /proc/meminfo  <span class="comment"># 查看内存信息</span></span><br><span class="line">uname -r <span class="comment"># 查看内核信息，Docker要求CentOS必须是64位，且内核是3.10及以上</span></span><br><span class="line">sudo reboot <span class="comment"># 重启,sudo表示以管理员root身份执行</span></span><br><span class="line">sudo halt <span class="comment"># 关机</span></span><br></pre></td></tr></table></figure><h4 id="5-2-安装软件"><a href="#5-2-安装软件" class="headerlink" title="5.2 安装软件"></a>5.2 安装软件</h4><p>​    使用yum，是一个基于rpm的软件包管理工具</p><p>​    用来安装软件包，可以自动解决软件包之间的依赖关系</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yum install 软件包名  <span class="comment">#　安装</span></span><br><span class="line">yum remove 软件包名 <span class="comment"># 卸载</span></span><br></pre></td></tr></table></figure><h2 id="三、Docker安装"><a href="#三、Docker安装" class="headerlink" title="三、Docker安装"></a>三、Docker安装</h2><h3 id="1-安装"><a href="#1-安装" class="headerlink" title="1. 安装"></a>1. 安装</h3><p>​    Docker版本：社区版CE、企业版EE</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 设置yum源</span></span><br><span class="line">yum install -y yum-utils device-mapper-persistent-data lvm2</span><br><span class="line">yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo</span><br><span class="line">yum makecache fast</span><br><span class="line"><span class="comment"># 安装Docker-CE</span></span><br><span class="line">yum -y install docker-ce</span><br></pre></td></tr></table></figure><h3 id="2-启动-停止"><a href="#2-启动-停止" class="headerlink" title="2. 启动/停止"></a>2. 启动/停止</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">docker version <span class="comment"># 查看版本</span></span><br><span class="line">systemctl start docker <span class="comment">#　启动</span></span><br><span class="line">systemctl stop docker　<span class="comment"># 停止</span></span><br><span class="line">systemctl status docker <span class="comment">#　查看状态</span></span><br><span class="line">systemctl restart docker　　<span class="comment"># 重启</span></span><br><span class="line">systemctl <span class="built_in">enable</span> docker  <span class="comment"># 设置开机自动启动</span></span><br><span class="line"><span class="comment"># 验证，运行hello-world</span></span><br><span class="line">docker run hello-world  <span class="comment"># 下载hello-world镜像并运行</span></span><br></pre></td></tr></table></figure><h3 id="3-配置Docker镜像加速"><a href="#3-配置Docker镜像加速" class="headerlink" title="3. 配置Docker镜像加速"></a>3. 配置Docker镜像加速</h3><p>​    使用阿里云提供的镜像加速（镜像仓库），也可以使用网易云等</p><p>​    步骤：</p><ol><li><p>注册并登陆“阿里云的开发者平台” <span class="exturl" data-url="aHR0cDovL2Rldi5hbGl5dW4uY29tLw==">http://dev.aliyun.com<i class="fa fa-external-link-alt"></i></span></p></li><li><p>查看专属的加速器地址</p></li><li><p>配置自己的Docker加速器</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">vi /etc/docker/daemon.json</span><br><span class="line">&#123;</span><br><span class="line">      <span class="string">&quot;registry-mirrors&quot;</span>: [<span class="string">&quot;https://sswv6yx0.mirror.aliyuncs.com&quot;</span>]</span><br><span class="line">    &#125;</span><br><span class="line">systemctl daemon-reload</span><br><span class="line">systemctl restart docker</span><br></pre></td></tr></table></figure><h2 id="四、Docker操作"><a href="#四、Docker操作" class="headerlink" title="四、Docker操作"></a>四、Docker操作</h2></li></ol><p>​    输入<code>docker</code>可以查看Docker的命令用法，输入<code>docker COMMAND --help</code>查看指定命令的详细用法</p><h3 id="1-镜像操作"><a href="#1-镜像操作" class="headerlink" title="1. 镜像操作"></a>1. 镜像操作</h3><table><thead><tr><th>操作</th><th>命令</th><th>说明</th></tr></thead><tbody><tr><td>查找</td><td>docker search 关键字</td><td>可以在Docker Hub网站查看镜像的详细信息，如镜像的tag标签</td></tr><tr><td>抽取</td><td>docker pull 镜像名:tag</td><td>:tag表示软件的版本，如果不指定默认是latest</td></tr><tr><td>列表</td><td>docker images</td><td>查看所有本地镜像</td></tr><tr><td>获取元信息</td><td>docker inspect 镜像id</td><td>获取镜像的元信息，详细信息</td></tr><tr><td>删除</td><td>docker rmi -f 镜像id或镜像名:tag</td><td>删除指定的本地镜像，-f表示强制删除</td></tr></tbody></table><h3 id="2-容器操作"><a href="#2-容器操作" class="headerlink" title="2. 容器操作"></a>2. 容器操作</h3><table><thead><tr><th>操作</th><th>命令</th><th>说明</th></tr></thead><tbody><tr><td>运行</td><td>docker run –name 容器名 -i -t -p 主机端口:容器端口  -d  -v 主机目录:容器目录:ro 镜像id或镜像名称:tag</td><td>–name 指定容器名，名称自定义，如果不指定会自动命名； -i 以交互模式运行，即以交互模式运行容器；-t 分配一个伪终端，即命令行，通常组合使用-it ；-p 指定端口映射，将主机端口映射到容器内的端口；-d 表示后台运行，即守护式运行容器；-v 指定挂载主机目录到容器目录，默认为rw读写模式，ro表示只读</td></tr><tr><td>列表</td><td>docker ps -a  -q</td><td>查看正在运行的容器，-a表示显示所有容器，-q表示只显示容器id</td></tr><tr><td>启动</td><td>docker start 容器id或容器名称</td><td>启动容器</td></tr><tr><td>停止</td><td>docker stop 容器id或容器名称</td><td>停止正在运行的容器</td></tr><tr><td>删除</td><td>docker rm -f 容器id或容器名称</td><td>删除容器，-f表示强制删除</td></tr><tr><td>日志</td><td>docker logs 容器id或容器名称</td><td>获取容器的日志</td></tr><tr><td>在容器中执行</td><td>docker exec -it 容器id或容器名称 /bin/bash</td><td>进入正在运行的容器中并开启一个交互模式的终端，可以在容器中执行操作</td></tr><tr><td>拷贝文件</td><td>docker cp 主机中的文件路径 容器id或容器名称:容器路径；docker cp 容器id或容器名称:容器中的文件路径 主机路径</td><td>将文件中的文件拷贝到容器中；将容器中的文件拷贝到主机中</td></tr><tr><td>获取元信息</td><td>docker inspect 容器id</td><td>获取容器的元信息</td></tr><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr></tbody></table><p>以CentOS为例：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">docker search centos</span><br><span class="line">docker pull centos</span><br><span class="line">docker run --name mycentos -it centos:latest  <span class="comment"># 根据centos:latest镜像运行容器，并以交互模式进入容器中</span></span><br><span class="line"><span class="comment"># 实际上是在Docker容器中运行一个精简版的CentOS系统 </span></span><br><span class="line"><span class="built_in">exit</span> <span class="comment"># 退出并关闭容器</span></span><br><span class="line">docker ps -a</span><br><span class="line">docker start mycentos</span><br><span class="line">docker stop mycentos</span><br><span class="line">docker rm mycentos</span><br><span class="line">docker rm -f $(docker ps -aq)  <span class="comment"># 强制删除所有容器</span></span><br></pre></td></tr></table></figure><p>​    <strong>注：Docker容器内实际上是运行着一个精简版的Linux系统</strong></p><p>以tomcat为例：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 示例1：基本用法</span></span><br><span class="line">docker search tomcat</span><br><span class="line">docker pull tomcat</span><br><span class="line">docker run --name mytomcat -p 8888:8080 -d tomcat </span><br><span class="line"><span class="comment"># 测试：http://宿主机地址：8888</span></span><br><span class="line">docker stop mytomcat</span><br><span class="line">docker ps -a</span><br><span class="line">docker start mytomcat</span><br><span class="line"></span><br><span class="line"><span class="comment"># 示例2：拷贝文件和挂载目录</span></span><br><span class="line">docker run -p 8080:8080 -d tomcat</span><br><span class="line">docker <span class="built_in">exec</span> -it 70cba924861c  /bin/bash</span><br><span class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/tomcat/webapps/ROOT</span><br><span class="line"><span class="built_in">exit</span></span><br><span class="line"><span class="built_in">echo</span> welcome to tomcat &gt; index.jsp</span><br><span class="line">docker cp index.jsp 70cba924861c:/usr/<span class="built_in">local</span>/tomcat/webapps/ROOT  <span class="comment"># 将宿主机中的文件拷贝到容器中指定的目录中</span></span><br><span class="line"><span class="comment">#　部署web项目，将war文件放到容器中</span></span><br><span class="line">docker cp spring-web.war 70cba924861c:/usr/<span class="built_in">local</span>/tomcat/webapps</span><br><span class="line"></span><br><span class="line"><span class="comment"># 问题：如果项目更改了需要重新拷贝war文件，太麻烦，可以直接挂载目录（也称为数据卷Volume）</span></span><br><span class="line">docker run \</span><br><span class="line">-p 8080:8080 \</span><br><span class="line">-v /my/tomcat/webapps/spring-web.war:/usr/<span class="built_in">local</span>/tomcat/webapps/spring-web.war \</span><br><span class="line">-v /my/tomcat/data:/usr/<span class="built_in">local</span>/tomcat/dataVolume:ro \</span><br><span class="line">-d tomcat</span><br><span class="line"></span><br><span class="line"><span class="comment"># 示例3：启动多个容器，一个镜像可以启动多个容器，互相隔离、独立</span></span><br><span class="line">docker run -p 8081:8080 -d tomcat</span><br><span class="line">docker run -p 8082:8080 -d tomcat</span><br><span class="line">docker run -p 8083:8080 -d tomcat</span><br></pre></td></tr></table></figure><h3 id="3-镜像的分层结构"><a href="#3-镜像的分层结构" class="headerlink" title="3. 镜像的分层结构"></a>3. 镜像的分层结构</h3><p>​    tomcat镜像为什么这么大？</p><p>​    镜像是一种轻量级、可执行的独立软件包，<strong>用来打包软件运行环境和基于运行环境的软件，包含运行某个软件所需要的所有内容。</strong>    </p><p>​    基于UnionFS联合文件系统，采用分层结构，一层一层的堆叠起来，像一个同心圆，但从外面来说，只能看到最外层的文件系统（镜像层）</p><p><img src="https://upload-images.jianshu.io/upload_images/15264282-b50762944fbb57be.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="镜像的分层结构"></p><p>分层结构：共享资源、便于复用（许多镜像都是从相同的Base基础镜像构建而来的，基础镜像只需要保存一份）</p><p>​    镜像都是只读的，而由镜像生成的容器是可修改的</p><h3 id="4-创建镜像"><a href="#4-创建镜像" class="headerlink" title="4. 创建镜像"></a>4. 创建镜像</h3><p>​    有时从Docker镜像仓库中下载的镜像不能满足我们的要求，此时可以基于这个镜像（基础镜像）封装一个自己的镜像</p><p>​    两种方式：</p><ul><li>更新镜像：使用docker commit命令</li><li>构建镜像：使用docker build命令，需要创建Dockerfile文件</li></ul><h4 id="4-1-更新镜像"><a href="#4-1-更新镜像" class="headerlink" title="4.1 更新镜像"></a>4.1 更新镜像</h4><p>​    先使用基础镜像创建一个容器，然后对容器进行修改，最后使用commit命令提交为一个新的镜像</p><p>​    步骤：</p><ol><li><p>根据基础镜像，创建容器</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run --name mytomcat -p 8080:8080 -d tomcat</span><br></pre></td></tr></table></figure></li><li><p>修改容器</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker <span class="built_in">exec</span> -it bcd08edac78d  /bin/bash</span><br><span class="line"><span class="built_in">cd</span> webapps/ROOT</span><br><span class="line">rm -f index.jsp</span><br><span class="line"><span class="built_in">echo</span> welcome to tomcat &gt; index.html</span><br><span class="line"><span class="built_in">exit</span></span><br></pre></td></tr></table></figure></li><li><p>提交为新镜像，语法：<code>docker commit -m=&quot;描述消息&quot; -a=&quot;作者&quot; 容器id或容器名 镜像名:tag </code> </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker commit -m=<span class="string">&quot;修改默认索引页&quot;</span> -a=<span class="string">&quot;汤小洋&quot;</span> bcd08edac78d itany/tomcat:v1</span><br></pre></td></tr></table></figure></li><li><p>使用新镜像运行容器</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run --name tomcat_v1 -p:8080:8080 -d itany/tomcat:v1</span><br></pre></td></tr></table></figure><h4 id="4-2-构建镜像"><a href="#4-2-构建镜像" class="headerlink" title="4.2 构建镜像"></a>4.2 构建镜像</h4></li></ol><p>​    根据Dockerfile文件来自动构建镜像</p><p>​    Dockerfile是一个包含创建镜像所有命令的文本文件，使用docker build命令可以根据Dockerfile的内容创建镜像</p><p>​    步骤：</p><ol><li><p>创建一个Dockerfile文件<code>vi Dockerfile</code></p><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 基础镜像</span></span><br><span class="line"><span class="keyword">FROM</span> tomcat</span><br><span class="line"></span><br><span class="line"><span class="comment"># 作者</span></span><br><span class="line"><span class="keyword">MAINTAINER</span> tangxiaoyang@itany.com</span><br><span class="line"></span><br><span class="line"><span class="comment"># 执行命令</span></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> rm -f /usr/<span class="built_in">local</span>/tomcat/webapps/ROOT/index.jsp</span></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> <span class="built_in">echo</span> <span class="string">&quot;welcome to tomcat!&quot;</span> &gt; /usr/<span class="built_in">local</span>/tomcat/webapps/ROOT/index.html</span></span><br></pre></td></tr></table></figure></li><li><p>构建新镜像，语法：<code>docker build -f Dockerfile文件的路径  -t 镜像名:tag 命令执行的上下文</code> </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -f Dockerfile -t itany/tomcat:v2 .</span><br></pre></td></tr></table></figure></li><li><p>使用新镜像运行容器</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -p 9999:8080 -d itany/tomcat:v2</span><br></pre></td></tr></table></figure><h2 id="五、Dockerfile详解"><a href="#五、Dockerfile详解" class="headerlink" title="五、Dockerfile详解"></a>五、Dockerfile详解</h2></li></ol><h3 id="1-简介"><a href="#1-简介" class="headerlink" title="1. 简介"></a>1. 简介</h3><p>​    Dockerfile是用来构建Docker镜像的文件，是由一系列命令和参数构成的脚本</p><p>​    Dockerfile从FROM命令开始，紧接着各种命令、参数等，最终会生成一个新的镜像</p><p>​    使用Dockerfile构建镜像的步骤：</p><ol><li>编写Dockerfile文件</li><li>使用docker build构建镜像</li><li>使用docker run运行容器</li></ol><h3 id="2-用法"><a href="#2-用法" class="headerlink" title="2. 用法"></a>2. 用法</h3><h4 id="2-1-语法规则"><a href="#2-1-语法规则" class="headerlink" title="2.1 语法规则"></a>2.1 语法规则</h4><ul><li>指令必须要大写，且后面必须跟参数</li><li>第一条指令必须是FROM，指定Base Image 基础镜像</li><li>指令按从上往下的顺序，依次执行</li><li>每条指定都会创建一个新的镜像层并提交</li><li><code>#</code>表示注释</li></ul><h4 id="2-2-常用指令"><a href="#2-2-常用指令" class="headerlink" title="2.2 常用指令"></a>2.2 常用指令</h4><table><thead><tr><th>指令</th><th>解释</th></tr></thead><tbody><tr><td>FROM</td><td>指定基础镜像，即当前新镜像是基于哪个镜像的</td></tr><tr><td>MAINTAINER</td><td>指定作者</td></tr><tr><td>RUN</td><td>指定构建过程中要运行的命令</td></tr><tr><td>ENV</td><td>设置环境变量</td></tr><tr><td>WORKDIR</td><td>指定默认的工作目录，即进入容器后默认进入的目录</td></tr><tr><td>VOLUME</td><td>创建挂载点，也称容器数据卷，用于数据共享和持久化</td></tr><tr><td>CMD</td><td>指定容器启动时要运行的命令，与RUN不同的是，这些命令不是在镜像构建过程中执行的</td></tr><tr><td>ENTRYPOINT</td><td>指定容器启动时要运行的命令，与CMD有区别</td></tr><tr><td>COPY</td><td>拷贝文件/目录到镜像中</td></tr><tr><td>ADD</td><td>拷贝文件到镜像中，且会自动解压缩</td></tr><tr><td>EXPOSE</td><td>指定对外暴露的端口</td></tr></tbody></table><h3 id="3-案例"><a href="#3-案例" class="headerlink" title="3. 案例"></a>3. 案例</h3><h4 id="3-1-自定义centos镜像"><a href="#3-1-自定义centos镜像" class="headerlink" title="3.1 自定义centos镜像"></a>3.1 自定义centos镜像</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1.编写Dockerfile文件</span></span><br><span class="line">vi Dockerfile2</span><br><span class="line">FROM centos</span><br><span class="line"></span><br><span class="line">MAINTAINER tangxiaoyang@itany.com</span><br><span class="line"></span><br><span class="line">ENV MYPATH /usr/<span class="built_in">local</span>/centos</span><br><span class="line">RUN mkdir -p <span class="variable">$MYPATH</span></span><br><span class="line">WORKDIR <span class="variable">$MYPATH</span></span><br><span class="line"></span><br><span class="line">RUN yum -y install vim</span><br><span class="line"></span><br><span class="line">RUN yum -y install wget</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建挂载点，无法指定宿主机上对应的目录，是自动生成的</span></span><br><span class="line">VOLUME  [<span class="string">&quot;/data1&quot;</span>,<span class="string">&quot;/data2&quot;</span>]</span><br><span class="line"></span><br><span class="line">CMD [<span class="string">&quot;/bin/bash&quot;</span>]</span><br><span class="line"><span class="comment"># 2.使用docker build构建镜像</span></span><br><span class="line">docker build -f Dockerfile2 -t itany/centos:v1 . </span><br><span class="line"><span class="comment"># 3.使用docker run运行容器</span></span><br><span class="line">docker run -it b25b1dad795c</span><br><span class="line"><span class="comment"># 查看镜像的变更历史</span></span><br><span class="line">docker <span class="built_in">history</span> b25b1dad795c</span><br><span class="line"><span class="comment"># 验证挂载点：</span></span><br><span class="line">/var/lib/docker/volumes/0b001b4cc8db1ebbbb4c537c17a5c44adb700fb0e1b941bc82cc717c4ae196f6/_data</span><br><span class="line">/var/lib/docker/volumes/f020f5a5664bf68312be9f49a640f27ecfb49990b231aaf3d0eb7cb723fa0ddd/_data</span><br></pre></td></tr></table></figure><p>​    CMD和ENTRYPOINT的区别（面试题）：</p><ul><li><p>CMD</p><p>在Dockerfile中可以有多个CMD指令，但只有最后一条指令生效，所以一般只有一条CMD指令</p><p>CMD指令在被docker run之后的参数覆盖</p><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">vi aaa</span><br><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> [<span class="string">&quot;/bin/ls&quot;</span>]</span></span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> [<span class="string">&quot;/bin/bash&quot;</span>]</span></span><br><span class="line">docker build -f aaa -t itany/aaa . </span><br><span class="line">docker <span class="keyword">run</span><span class="bash"> -it itany/aaa</span></span><br><span class="line">docker <span class="keyword">run</span><span class="bash"> -it itany/aaa /bin/<span class="built_in">pwd</span></span></span><br></pre></td></tr></table></figure></li><li><p>ENTRYPOINT</p><p>docker run之后的参数会被作为ENTRYPOINT指令的参数，组合形成新的命令</p><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">vi bbb</span><br><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">ENTRYPOINT</span><span class="bash"> [<span class="string">&quot;/bin/ls&quot;</span>,<span class="string">&quot;/usr/local&quot;</span>]</span></span><br><span class="line">docker build -f bbb -t itany/bbb .</span><br><span class="line">docker <span class="keyword">run</span><span class="bash"> -it itany/bbb</span></span><br><span class="line">docker <span class="keyword">run</span><span class="bash"> -it itany/bbb -l  <span class="comment">#  ls /usr/local -l</span></span></span><br></pre></td></tr></table></figure><h4 id="3-2-自定义tomcat镜像"><a href="#3-2-自定义tomcat镜像" class="headerlink" title="3.2 自定义tomcat镜像"></a>3.2 自定义tomcat镜像</h4></li></ul><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 准备工作 </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 1.编写Dockerfile文件</span></span><br><span class="line">vi Dockerfile</span><br><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">MAINTAINER</span> tangxiaoyang@itany.com</span><br><span class="line"></span><br><span class="line"><span class="comment"># 拷贝文件，文件必须与Dockerfile在同一目录下</span></span><br><span class="line"><span class="keyword">COPY</span><span class="bash"> teacher.txt /usr/<span class="built_in">local</span></span></span><br><span class="line"><span class="keyword">ADD</span><span class="bash"> jdk-8u171-linux-x64.tar.gz /usr/<span class="built_in">local</span></span></span><br><span class="line"><span class="keyword">ADD</span><span class="bash"> apache-tomcat-8.5.30.tar.gz /usr/<span class="built_in">local</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 配置环境变量</span></span><br><span class="line"><span class="keyword">ENV</span> JAVA_HOME /usr/local/jdk1.<span class="number">8.0</span>_171</span><br><span class="line"><span class="keyword">ENV</span> CLASSPATH .:$JAVA_HOME/lib</span><br><span class="line"><span class="keyword">ENV</span> CATALINA_HOME /usr/local/apache-tomcat-<span class="number">8.5</span>.<span class="number">30</span></span><br><span class="line"><span class="keyword">ENV</span> PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin</span><br><span class="line"></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="bash"> <span class="variable">$CATALINA_HOME</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> yum -y install vim</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">EXPOSE</span> <span class="number">8080</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> [<span class="string">&quot;catalina.sh&quot;</span>, <span class="string">&quot;run&quot;</span>]</span></span><br><span class="line"><span class="comment"># 2.使用docker build构建镜像</span></span><br><span class="line">docker build -t itany/tomcat:<span class="number">1.0</span> .</span><br><span class="line"><span class="comment"># 3.使用docker run运行容器</span></span><br><span class="line">docker <span class="keyword">run</span><span class="bash"> \</span></span><br><span class="line"><span class="bash">--name mytomcat \</span></span><br><span class="line"><span class="bash">-p 8080:8080 \</span></span><br><span class="line"><span class="bash">-v /my/tomcat/webapps/spring-web.war:/usr/<span class="built_in">local</span>/apache-tomcat-8.5.30/webapps/spring-web.war \</span></span><br><span class="line"><span class="bash">-d itany/tomcat:1.0</span></span><br></pre></td></tr></table></figure><h2 id="六、使用Docker搭建环境"><a href="#六、使用Docker搭建环境" class="headerlink" title="六、使用Docker搭建环境"></a>六、使用Docker搭建环境</h2><h3 id="1-安装MySQL"><a href="#1-安装MySQL" class="headerlink" title="1. 安装MySQL"></a>1. 安装MySQL</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1.拉取镜像</span></span><br><span class="line">docker pull mysql:5.7</span><br><span class="line"><span class="comment"># 2.运行容器</span></span><br><span class="line">docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7</span><br><span class="line">docker <span class="built_in">exec</span> -it mysql /bin/bash</span><br><span class="line">find / -name <span class="string">&quot;*mysql*&quot;</span></span><br><span class="line"><span class="built_in">exit</span></span><br><span class="line"><span class="comment"># 3.创建用于挂载的目录</span></span><br><span class="line">mkdir -p /my/mysql/conf <span class="comment"># 挂载配置文件</span></span><br><span class="line">mkdir -p /my/mysql/data <span class="comment"># 挂载数据文件</span></span><br><span class="line">mkdir -p /my/mysql/logs <span class="comment"># 挂载日志文件</span></span><br><span class="line"><span class="comment"># 4.拷贝配置文件并修改</span></span><br><span class="line">docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf /my/mysql/conf/</span><br><span class="line">vi /my/mysql/conf/mysqld.conf</span><br><span class="line">character-set-server=utf8</span><br><span class="line"><span class="comment"># 5.重新运行容器</span></span><br><span class="line">docker rm -f mysql  <span class="comment"># 删除原来的容器</span></span><br><span class="line">docker run \</span><br><span class="line">--name mysql \</span><br><span class="line">-p 3306:3306 \</span><br><span class="line">-v /my/mysql/conf:/etc/mysql/mysql.conf.d/ \</span><br><span class="line">-v /my/mysql/data:/var/lib/mysql \</span><br><span class="line">-v /my/mysql/logs:/logs \</span><br><span class="line">-e MYSQL_ROOT_PASSWORD=root \</span><br><span class="line">-d mysql:5.7</span><br><span class="line"><span class="comment"># 6.访问</span></span><br><span class="line"><span class="comment"># 本地访问</span></span><br><span class="line">docker <span class="built_in">exec</span> -it mysql /bin/bash</span><br><span class="line">mysql -u root -p </span><br><span class="line"><span class="comment"># 远程访问</span></span><br><span class="line">mysql -u root -p -h 宿主机地址</span><br></pre></td></tr></table></figure><h3 id="2-安装Redis"><a href="#2-安装Redis" class="headerlink" title="2. 安装Redis"></a>2. 安装Redis</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1.拉取镜像</span></span><br><span class="line">docker pull redis</span><br><span class="line"><span class="comment"># 2.创建用于挂载的目录</span></span><br><span class="line">mkdir -p /my/redis/conf </span><br><span class="line">mkdir -p /my/redis/data</span><br><span class="line"><span class="comment"># 3.拷贝配置文件并修改</span></span><br><span class="line">wget http://download.redis.io/releases/redis-4.0.10.tar.gz</span><br><span class="line">tar zxf redis-4.0.10.tar.gz</span><br><span class="line">cp redis.conf /my/redis/conf/</span><br><span class="line">vi redis.conf</span><br><span class="line">requirepass itany</span><br><span class="line">appendonly yes</span><br><span class="line"><span class="comment"># 4.运行容器</span></span><br><span class="line">docker run \</span><br><span class="line">--name myredis \</span><br><span class="line">-p 6379:6379 \</span><br><span class="line">-v /my/redis/conf/redis.conf:/usr/<span class="built_in">local</span>/etc/redis/redis.conf \</span><br><span class="line">-v /my/redis/data:/data \</span><br><span class="line">-d redis redis-server /usr/<span class="built_in">local</span>/etc/redis/redis.conf</span><br><span class="line"><span class="comment"># 5.访问</span></span><br><span class="line"><span class="comment"># 本地访问</span></span><br><span class="line">docker <span class="built_in">exec</span> -it myredis /bin/bash</span><br><span class="line">redis-cli</span><br><span class="line"><span class="comment">#　远程访问</span></span><br><span class="line">使用RedisDesktopManager工具连接</span><br></pre></td></tr></table></figure><h3 id="3-安装Nginx"><a href="#3-安装Nginx" class="headerlink" title="3. 安装Nginx"></a>3. 安装Nginx</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1.拉取镜像</span></span><br><span class="line">docker pull nginx</span><br><span class="line"><span class="comment"># 2.运行容器</span></span><br><span class="line">docker run --name mynginx -p 80:80 -d nginx</span><br><span class="line"><span class="comment"># 3.创建用于挂载的目录</span></span><br><span class="line">mkdir -p /my/nginx  <span class="comment"># 挂载nginx所有数据</span></span><br><span class="line">mkdir -p /my/nginx/html <span class="comment"># 挂载nginx虚拟主机（网站html数据）</span></span><br><span class="line"><span class="comment"># 4.拷贝配置文件</span></span><br><span class="line">docker cp mynginx:/etc/nginx/nginx.conf /my/nginx  <span class="comment"># 拷贝主配置文件</span></span><br><span class="line">docker cp mynginx:/etc/nginx/conf.d /my/nginx <span class="comment"># 拷贝虚拟主机配置文件</span></span><br><span class="line"><span class="built_in">echo</span> welcome to nginx &gt; /my/nginx/html/index.html <span class="comment">#　自定义索引页</span></span><br><span class="line"><span class="comment"># 5.重启运行容器</span></span><br><span class="line">docker rm -f mynginx</span><br><span class="line">docker run \</span><br><span class="line">--name mynginx \</span><br><span class="line">-p 80:80 -p 443:443 \</span><br><span class="line">-v /my/nginx/nginx.conf:/etc/nginx/nginx.conf \</span><br><span class="line">-v /my/nginx/html:/usr/share/nginx/html:ro \</span><br><span class="line">-v /etc/nginx/conf.d:/usr/nginx/conf.d \</span><br><span class="line">-d nginx</span><br><span class="line"><span class="comment"># 6.测试</span></span><br><span class="line">http://宿主机地址</span><br></pre></td></tr></table></figure><h2 id="七、将本地镜像发布到阿里云"><a href="#七、将本地镜像发布到阿里云" class="headerlink" title="七、将本地镜像发布到阿里云"></a>七、将本地镜像发布到阿里云</h2><p>​    步骤：</p><ol><li><p>登陆“阿里云-开发者平台”，创建命名空间和镜像仓库</p></li><li><p>将镜像推送到阿里云</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 登陆阿里云的docker仓库</span></span><br><span class="line">docker login --username=tangyang8942@163.com registry.cn-hangzhou.aliyuncs.com</span><br><span class="line"><span class="comment"># 创建指定镜像的tag，归入某个仓库</span></span><br><span class="line">docker tag b25b1dad795c registry.cn-hangzhou.aliyuncs.com/itany/centos:v1.0</span><br><span class="line"><span class="comment"># 将镜像推送到仓库中</span></span><br><span class="line">docker push registry.cn-hangzhou.aliyuncs.com/itany/centos:v1.0</span><br></pre></td></tr></table></figure></li><li><p>拉取镜像</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull registry.cn-hangzhou.aliyuncs.com/itany/centos:v1.0</span><br></pre></td></tr></table></figure></li></ol>]]></content>
    
    
    <summary type="html">&lt;h2 id=&quot;一、Docker简介&quot;&gt;&lt;a href=&quot;#一、Docker简介&quot; class=&quot;headerlink&quot; title=&quot;一、Docker简介&quot;&gt;&lt;/a&gt;一、Docker简介&lt;/h2&gt;&lt;h3 id=&quot;1-Docker是什么？&quot;&gt;&lt;a href=&quot;#1-Docker是什么？&quot; class=&quot;headerlink&quot; title=&quot;1. Docker是什么？&quot;&gt;&lt;/a&gt;1. Docker是什么？&lt;/h3&gt;&lt;p&gt;​    产生背景：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;开发和运维之间因为环境不同而导致的矛盾（不同的操作系统、软件环境、应用配置等） DevOps&lt;/li&gt;
&lt;li&gt;集群环境下每台服务器都配置相同的环境，太麻烦&lt;/li&gt;
&lt;li&gt;解决“在我的机器上可以正常工作”的问题&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;​        Docker是一个开源的应用容器引擎，让开发者可以打包他们的应用以及依赖包到一个可移植的容器中，然后发布到任何流行的 Linux 机器上，也可以实现虚拟化。&lt;/p&gt;</summary>
    
    
    
    <category term="docker" scheme="https://www.kiilin.com/categories/docker/"/>
    
    
    <category term="docker" scheme="https://www.kiilin.com/tags/docker/"/>
    
  </entry>
  
  <entry>
    <title>使用kubeadm安装Kubernetes 1.13</title>
    <link href="https://www.kiilin.com/blog/k8s_install.html"/>
    <id>https://www.kiilin.com/blog/k8s_install.html</id>
    <published>2019-01-15T09:04:00.000Z</published>
    <updated>2020-12-31T02:06:41.547Z</updated>
    
    <content type="html"><![CDATA[<p>kubeadm是Kubernetes官方提供的用于快速安装Kubernetes集群的工具，伴随Kubernetes每个版本的发布都会同步更新，kubeadm会对集群配置方面的一些实践做调整，通过实验kubeadm可以学习到Kubernetes官方在集群配置上一些新的最佳实践。</p><a id="more"></a><h2 id="安装前准备"><a href="#安装前准备" class="headerlink" title="安装前准备."></a>安装前准备.</h2><h3 id="系统配置"><a href="#系统配置" class="headerlink" title="系统配置"></a>系统配置</h3><h4 id="同步各节点之间的时间"><a href="#同步各节点之间的时间" class="headerlink" title="同步各节点之间的时间"></a>同步各节点之间的时间</h4><p>安装 时间同步工具<code>ntpdate</code>   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install ntpdate -y</span><br></pre></td></tr></table></figure><p>同步各服务器之间时间   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ntpdate 0.asia.pool.ntp.org</span><br></pre></td></tr></table></figure><h4 id="准备主机修改主机名及DNS"><a href="#准备主机修改主机名及DNS" class="headerlink" title="准备主机修改主机名及DNS"></a>准备主机修改主机名及DNS</h4><p>在安装之前，需要先做如下准备。三台CentOS 7主机如下：   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 三台主机分别修改hostname</span></span><br><span class="line">hostnamectl set-hostname master</span><br><span class="line">hostnamectl set-hostname node1</span><br><span class="line">hostnamectl set-hostname node2</span><br><span class="line"></span><br><span class="line"><span class="comment"># 重启三台节点</span></span><br><span class="line">reboot</span><br><span class="line"></span><br><span class="line"><span class="comment"># 修改主机host 编辑下hosts文件，</span></span><br><span class="line"><span class="comment"># 给127.0.0.1添加hostname</span></span><br><span class="line">vi /etc/hosts </span><br><span class="line">192.168.0.121 master</span><br><span class="line">192.168.0.122 node1</span><br><span class="line">192.168.0.123 node2</span><br><span class="line"></span><br><span class="line"><span class="comment"># 编辑后结果</span></span><br><span class="line">127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 master</span><br><span class="line">::1         localhost localhost.localdomain localhost6 localhost6.localdomain6</span><br><span class="line"></span><br><span class="line">192.168.0.121 master</span><br><span class="line">192.168.0.122 node1</span><br><span class="line">192.168.0.123 node2</span><br></pre></td></tr></table></figure><h4 id="检查主机防火墙"><a href="#检查主机防火墙" class="headerlink" title="检查主机防火墙"></a>检查主机防火墙</h4><p>如果各个主机启用了防火墙，需要开放Kubernetes各个组件所需要的端口，可以查看<span class="exturl" data-url="aHR0cHM6Ly9rdWJlcm5ldGVzLmlvL2RvY3Mvc2V0dXAvaW5kZXBlbmRlbnQvaW5zdGFsbC1rdWJlYWRtLw==">Installing kubeadm<i class="fa fa-external-link-alt"></i></span>中的”Check required ports”一节。 这里简单起见在各节点禁用防火墙：   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop firewalld</span><br><span class="line">systemctl <span class="built_in">disable</span> firewalld</span><br></pre></td></tr></table></figure><h4 id="禁用SELINUX："><a href="#禁用SELINUX：" class="headerlink" title="禁用SELINUX："></a>禁用SELINUX：</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">setenforce 0</span><br></pre></td></tr></table></figure><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">vi /etc/selinux/config</span><br><span class="line">SELINUX=disabled</span><br></pre></td></tr></table></figure><p>创建<code>vi /etc/sysctl.d/k8s.conf</code>文件，添加如下内容：   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">net.bridge.bridge-nf-call-ip6tables = 1</span><br><span class="line">net.bridge.bridge-nf-call-iptables = 1</span><br><span class="line">net.ipv4.ip_forward = 1</span><br></pre></td></tr></table></figure><p>执行命令使修改生效。   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">modprobe br_netfilter</span><br><span class="line">sysctl -p /etc/sysctl.d/k8s.conf</span><br></pre></td></tr></table></figure><h3 id="kube-proxy开启ipvs的前置条件"><a href="#kube-proxy开启ipvs的前置条件" class="headerlink" title="kube-proxy开启ipvs的前置条件"></a>kube-proxy开启ipvs的前置条件</h3><p>由于ipvs已经加入到了内核的主干，所以为kube-proxy开启ipvs的前提需要加载以下的内核模块：   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ip_vs</span><br><span class="line">ip_vs_rr</span><br><span class="line">ip_vs_wrr</span><br><span class="line">ip_vs_sh</span><br><span class="line">nf_conntrack_ipv4</span><br></pre></td></tr></table></figure><p>在所有的Kubernetes节点上执行以下脚本:   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">cat &gt; /etc/sysconfig/modules/ipvs.modules &lt;&lt;<span class="string">EOF</span></span><br><span class="line"><span class="string">#!/bin/bash</span></span><br><span class="line"><span class="string">modprobe -- ip_vs</span></span><br><span class="line"><span class="string">modprobe -- ip_vs_rr</span></span><br><span class="line"><span class="string">modprobe -- ip_vs_wrr</span></span><br><span class="line"><span class="string">modprobe -- ip_vs_sh</span></span><br><span class="line"><span class="string">modprobe -- nf_conntrack_ipv4</span></span><br><span class="line"><span class="string">EOF</span></span><br><span class="line">chmod 755 /etc/sysconfig/modules/ipvs.modules &amp;&amp; bash /etc/sysconfig/modules/ipvs.modules &amp;&amp; lsmod | grep -e ip_vs -e nf_conntrack_ipv4</span><br></pre></td></tr></table></figure><p>上面脚本创建了的<code>/etc/sysconfig/modules/ipvs.modules</code>文件，保证在节点重启后能自动加载所需模块。 使用<code>lsmod | grep -e ip_vs -e nf_conntrack_ipv4</code>命令查看是否已经正确加载所需的内核模块。</p><p>接下来还需要确保各个节点上已经安装了<code>ipset</code>软件包<code>yum install ipset</code>。 为了便于查看<code>ipvs</code>的代理规则，最好安装一下管理工具<code>ipvsadm</code> <code>yum install ipvsadm</code>。</p><p>如果以上前提条件如果不满足，则即使<code>kube-proxy</code>的配置开启了ipvs模式，也会退回到<code>iptables</code>模式。</p><h3 id="安装docker"><a href="#安装docker" class="headerlink" title="安装docker"></a>安装docker</h3><p>卸载旧版本   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">yum remove  docker \</span><br><span class="line">            docker-client \</span><br><span class="line">            docker-client-latest \</span><br><span class="line">            docker-common \</span><br><span class="line">            docker-latest \</span><br><span class="line">            docker-latest-logrotate \</span><br><span class="line">            docker-logrotate \</span><br><span class="line">            docker-selinux \</span><br><span class="line">            docker-engine-selinux \</span><br><span class="line">            docker-engine</span><br></pre></td></tr></table></figure><p>安装一些必要的系统工具：   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y yum-utils device-mapper-persistent-data lvm2</span><br></pre></td></tr></table></figure><p>添加软件源信息：   </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo</span><br></pre></td></tr></table></figure><p>更新 yum 缓存：      </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum makecache fast</span><br></pre></td></tr></table></figure><p>安装 Docker-ce：      </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install docker-ce</span><br></pre></td></tr></table></figure><p>启动 Docker 后台服务    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">systemctl start docker</span><br><span class="line">systemctl <span class="built_in">enable</span> docker</span><br></pre></td></tr></table></figure><p>测试运行 hello-world    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run hello-world</span><br></pre></td></tr></table></figure><p>确认一下iptables filter表中FOWARD链的默认策略(pllicy)为ACCEPT。    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">iptables -nvL</span><br></pre></td></tr></table></figure><blockquote><p>Docker从1.13版本开始调整了默认的防火墙规则，禁用了iptables filter表中FOWARD链，这样会引起Kubernetes集群中跨Node的Pod无法通信。但这里通过安装docker 1806，发现默认策略又改回了ACCEPT，这个不知道是从哪个版本改回的，因为我们线上版本使用的1706还是需要手动调整这个策略的。</p></blockquote><h2 id="使用kubeadm部署Kubernetes"><a href="#使用kubeadm部署Kubernetes" class="headerlink" title="使用kubeadm部署Kubernetes"></a>使用kubeadm部署Kubernetes</h2><h3 id="安装kubeadm和kubelet"><a href="#安装kubeadm和kubelet" class="headerlink" title="安装kubeadm和kubelet"></a>安装kubeadm和kubelet</h3><p>下面在各节点安装kubeadm和kubelet：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">cat &lt;&lt;<span class="string">EOF &gt; /etc/yum.repos.d/kubernetes.repo</span></span><br><span class="line"><span class="string">[kubernetes]</span></span><br><span class="line"><span class="string">name=Kubernetes</span></span><br><span class="line"><span class="string">baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64</span></span><br><span class="line"><span class="string">enabled=1</span></span><br><span class="line"><span class="string">gpgcheck=1</span></span><br><span class="line"><span class="string">repo_gpgcheck=1</span></span><br><span class="line"><span class="string">gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg</span></span><br><span class="line"><span class="string">        https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg</span></span><br><span class="line"><span class="string">EOF</span></span><br></pre></td></tr></table></figure><p>测试地址<code>https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64</code>是否可用    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64</span><br></pre></td></tr></table></figure><p>如果不可用需要将地址替换为阿里镜像    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/</span><br></pre></td></tr></table></figure><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">cat &lt;&lt;<span class="string">EOF &gt; /etc/yum.repos.d/kubernetes.repo</span></span><br><span class="line"><span class="string">[kubernetes]</span></span><br><span class="line"><span class="string">name=Kubernetes</span></span><br><span class="line"><span class="string">baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/</span></span><br><span class="line"><span class="string">enabled=1</span></span><br><span class="line"><span class="string">gpgcheck=1</span></span><br><span class="line"><span class="string">repo_gpgcheck=1</span></span><br><span class="line"><span class="string">gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg </span></span><br><span class="line"><span class="string">        https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg</span></span><br><span class="line"><span class="string">EOF</span></span><br></pre></td></tr></table></figure><p>安装kubelet kubeadm kubectl</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yum makecache fast</span><br><span class="line">yum install -y kubelet kubeadm kubectl</span><br></pre></td></tr></table></figure><blockquote><p>从安装结果可以看出还安装了cri-tools, kubernetes-cni, socat三个依赖：<br>官方从Kubernetes 1.9开始就将cni依赖升级到了0.6.0版本，在当前1.12中仍然是这个版本<br>socat是kubelet的依赖<br>cri-tools是CRI(Container Runtime Interface)容器运行时接口的命令行工具</p></blockquote><p>运行<code>kubelet –help</code>可以看到原来kubelet的绝大多数命令行flag参数都被DEPRECATED了，如：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line">--address 0.0.0.0   The IP address <span class="keyword">for</span> the Kubelet to serve on (<span class="built_in">set</span> to 0.0.0.0 <span class="keyword">for</span> all IPv4 interfaces and `::` <span class="keyword">for</span> all IPv6 interfaces) (default 0.0.0.0) (DEPRECATED: This parameter should be <span class="built_in">set</span> via the config file specified by the Kubelet<span class="string">&#x27;s --config flag. See https://kubernetes.io/docs/tasks/administer-cluster/kubelet-config-file/ for more information.)</span></span><br><span class="line"><span class="string">......</span></span><br></pre></td></tr></table></figure><p>而官方推荐我们使用–config指定配置文件，并在配置文件中指定原来这些flag所配置的内容。具体内容可以查看这里<span class="exturl" data-url="aHR0cHM6Ly9rdWJlcm5ldGVzLmlvL2RvY3MvdGFza3MvYWRtaW5pc3Rlci1jbHVzdGVyL2t1YmVsZXQtY29uZmlnLWZpbGUv">Set Kubelet parameters via a config file。<i class="fa fa-external-link-alt"></i></span>这也是Kubernetes为了支持动态Kubelet配置（Dynamic Kubelet Configuration）才这么做的，参考<span class="exturl" data-url="aHR0cHM6Ly9rdWJlcm5ldGVzLmlvL2RvY3MvdGFza3MvYWRtaW5pc3Rlci1jbHVzdGVyL3JlY29uZmlndXJlLWt1YmVsZXQv">Reconfigure a Node’s Kubelet in a Live Cluster<i class="fa fa-external-link-alt"></i></span>。</p><p>kubelet的配置文件必须是json或yaml格式，具体可查看<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2t1YmVybmV0ZXMva3ViZXJuZXRlcy9ibG9iL3JlbGVhc2UtMS4xMC9wa2cva3ViZWxldC9hcGlzL2t1YmVsZXRjb25maWcvdjFiZXRhMS90eXBlcy5nbw==">这里<i class="fa fa-external-link-alt"></i></span>。</p><p>Kubernetes 1.8开始要求关闭系统的Swap，如果不关闭，默认配置下kubelet将无法启动。</p><blockquote><p>关闭系统的Swap方法如下:    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">swapoff -a</span><br></pre></td></tr></table></figure><p>修改 <code>vi /etc/fstab</code> 文件，注释掉 SWAP 的自动挂载，使用<code>free -m</code>确认swap已经关闭。 swappiness参数调整，修改<code>vi /etc/sysctl.d/k8s.conf</code>添加下面一行：</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vm.swappiness=0</span><br></pre></td></tr></table></figure><p>执行<code>sysctl -p /etc/sysctl.d/k8s.conf</code>使修改生效。</p></blockquote><p>因为这里本次用于测试两台主机上还运行其他服务，关闭swap可能会对其他服务产生影响，所以这里修改kubelet的配置去掉这个限制。 之前的Kubernetes版本我们都是通过kubelet的启动参数<code>–fail-swap-on=false</code>去掉这个限制的。前面已经分析了Kubernetes不再推荐使用启动参数，而推荐使用配置文件。 所以这里我们改成配置文件配置的形式。</p><p>查看<code>cat /etc/systemd/system/kubelet.service.d/10-kubeadm.conf</code>，看到了下面的内容：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Note: This dropin only works with kubeadm and kubelet v1.11+</span></span><br><span class="line">[Service]</span><br><span class="line">Environment=<span class="string">&quot;KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf&quot;</span></span><br><span class="line">Environment=<span class="string">&quot;KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml&quot;</span></span><br><span class="line"><span class="comment"># This is a file that &quot;kubeadm init&quot; and &quot;kubeadm join&quot; generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically</span></span><br><span class="line">EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env</span><br><span class="line"><span class="comment"># This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use</span></span><br><span class="line"><span class="comment"># the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.</span></span><br><span class="line">EnvironmentFile=-/etc/sysconfig/kubelet</span><br><span class="line">ExecStart=</span><br><span class="line">ExecStart=/usr/bin/kubelet <span class="variable">$KUBELET_KUBECONFIG_ARGS</span> <span class="variable">$KUBELET_CONFIG_ARGS</span> <span class="variable">$KUBELET_KUBEADM_ARGS</span> <span class="variable">$KUBELET_EXTRA_ARGS</span></span><br></pre></td></tr></table></figure><p>上面显示kubeadm部署的kubelet的配置文件<code>–config=/var/lib/kubelet/config.yaml</code>，实际去查看<code>/var/lib/kubelet</code>和这个<code>config.yaml</code>的配置文件都没有被创建。 可以猜想肯定是运行kubeadm初始化集群时会自动生成这个配置文件，而如果我们不关闭Swap的话，第一次初始化集群肯定会失败的。</p><p>所以还是老老实实的回到使用kubelet的启动参数<code>–fail-swap-on=false</code>去掉必须关闭Swap的限制。 修改<code>vi /etc/sysconfig/kubelet</code>，加入：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">KUBELET_EXTRA_ARGS=--fail-swap-on=<span class="literal">false</span></span><br></pre></td></tr></table></figure><h3 id="使用kubeadm-init初始化集群"><a href="#使用kubeadm-init初始化集群" class="headerlink" title="使用kubeadm init初始化集群"></a>使用kubeadm init初始化集群</h3><p>在各节点开机启动kubelet服务：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl <span class="built_in">enable</span> kubelet.service</span><br></pre></td></tr></table></figure><p>接下来使用kubeadm初始化集群，选择master作为Master Node，在master上执行下面的命令：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">kubeadm init \</span><br><span class="line">  --kubernetes-version=v1.13.0 \</span><br><span class="line">  --pod-network-cidr=10.244.0.0/16 \</span><br><span class="line">  --apiserver-advertise-address=192.168.0.121</span><br></pre></td></tr></table></figure><p>因为我们选择flannel作为Pod网络插件，所以上面的命令指定<code>–pod-network-cidr=10.244.0.0/16</code>。</p><p>执行时报了下面的错误：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[init] Using Kubernetes version: v1.13.0</span><br><span class="line">[preflight] Running pre-flight checks</span><br><span class="line">   [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.09.0. Latest validated version: 18.06</span><br><span class="line">error execution phase preflight: [preflight] Some fatal errors occurred:</span><br><span class="line">   [ERROR Swap]: running with swap on is not supported. Please <span class="built_in">disable</span> swap</span><br><span class="line">[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`</span><br></pre></td></tr></table></figure><p>有一个错误信息是<code>running with swap on is not supported. Please disable swap</code>。因为我们决定配置<code>failSwapOn: false</code>，所以重新添加<code>–ignore-preflight-errors=Swap</code>参数忽略这个错误，重新运行。    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">kubeadm init \</span><br><span class="line">   --kubernetes-version=v1.13.0 \</span><br><span class="line">   --pod-network-cidr=10.244.0.0/16 \</span><br><span class="line">   --apiserver-advertise-address=192.168.0.121 \</span><br><span class="line">   --ignore-preflight-errors=Swap</span><br></pre></td></tr></table></figure><blockquote><p>如果出现以下错误提示    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">[init] Using Kubernetes version: v1.13.0</span><br><span class="line">[preflight] Running pre-flight checks</span><br><span class="line">[WARNING Swap]: running with swap on is not supported. Please <span class="built_in">disable</span> swap</span><br><span class="line">[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.09.0. Latest validated version: 18.06</span><br><span class="line">[preflight] Pulling images required <span class="keyword">for</span> setting up a Kubernetes cluster</span><br><span class="line">[preflight] This might take a minute or two, depending on the speed of your internet connection</span><br><span class="line">[preflight] You can also perform this action <span class="keyword">in</span> beforehand using <span class="string">&#x27;kubeadm config images pull&#x27;</span></span><br><span class="line">error execution phase preflight: [preflight] Some fatal errors occurred:</span><br><span class="line">[ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-apiserver:v1.13.0: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled <span class="keyword">while</span> waiting <span class="keyword">for</span> connection (Client.Timeout exceeded <span class="keyword">while</span> awaiting headers)</span><br><span class="line">, error: <span class="built_in">exit</span> status 1</span><br><span class="line">[ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-controller-manager:v1.13.0: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled <span class="keyword">while</span> waiting <span class="keyword">for</span> connection (Client.Timeout exceeded <span class="keyword">while</span> awaiting headers)</span><br><span class="line">, error: <span class="built_in">exit</span> status 1</span><br><span class="line">[ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-scheduler:v1.13.0: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled <span class="keyword">while</span> waiting <span class="keyword">for</span> connection (Client.Timeout exceeded <span class="keyword">while</span> awaiting headers)</span><br><span class="line">, error: <span class="built_in">exit</span> status 1</span><br><span class="line">[ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-proxy:v1.13.0: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled <span class="keyword">while</span> waiting <span class="keyword">for</span> connection (Client.Timeout exceeded <span class="keyword">while</span> awaiting headers)</span><br><span class="line">, error: <span class="built_in">exit</span> status 1</span><br><span class="line">[ERROR ImagePull]: failed to pull image k8s.gcr.io/pause:3.1: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled <span class="keyword">while</span> waiting <span class="keyword">for</span> connection (Client.Timeout exceeded <span class="keyword">while</span> awaiting headers)</span><br><span class="line">, error: <span class="built_in">exit</span> status 1</span><br><span class="line">[ERROR ImagePull]: failed to pull image k8s.gcr.io/etcd:3.2.24: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled <span class="keyword">while</span> waiting <span class="keyword">for</span> connection (Client.Timeout exceeded <span class="keyword">while</span> awaiting headers)</span><br><span class="line">, error: <span class="built_in">exit</span> status 1</span><br><span class="line">[ERROR ImagePull]: failed to pull image k8s.gcr.io/coredns:1.2.6: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled <span class="keyword">while</span> waiting <span class="keyword">for</span> connection (Client.Timeout exceeded <span class="keyword">while</span> awaiting headers)</span><br><span class="line">, error: <span class="built_in">exit</span> status 1</span><br><span class="line">[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`</span><br></pre></td></tr></table></figure><p>是因为 导入docker k8s 镜像 这些镜像默认在google是无法访问到的 需要从dockerhub官方网站上把需要的镜像先下载到本地 然后修改镜像的tag 再执行<code>kubeadm init  kubeadm</code>首先会查看本机是否存在相应的docker镜像 如果有就直接启动本机的镜像如果没有就会从<span class="exturl" data-url="aHR0cHM6Ly9rOHMuZ2NyLmlvLw==">https://k8s.gcr.io<i class="fa fa-external-link-alt"></i></span>仓库中下载相关镜像<br>上面的提示信息中包含所需要的镜像的名称和tag     根据提示所需的名称和tag来从dockerhub中下载镜像到本机</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 从dockerhub下载需要的镜像</span></span><br><span class="line">docker pull mirrorgooglecontainers/kube-apiserver-amd64:v1.13.0</span><br><span class="line">docker pull mirrorgooglecontainers/kube-controller-manager-amd64:v1.13.0</span><br><span class="line">docker pull mirrorgooglecontainers/kube-scheduler-amd64:v1.13.0</span><br><span class="line">docker pull mirrorgooglecontainers/kube-proxy-amd64:v1.13.0</span><br><span class="line">docker pull mirrorgooglecontainers/pause:3.1</span><br><span class="line">docker pull mirrorgooglecontainers/etcd-amd64:3.2.24</span><br><span class="line">docker pull coredns/coredns:1.2.6</span><br><span class="line"><span class="comment"># 修改dockerhub镜像tag为k8s.gcr.io</span></span><br><span class="line">docker tag docker.io/mirrorgooglecontainers/kube-apiserver-amd64:v1.13.0 k8s.gcr.io/kube-apiserver:v1.13.0</span><br><span class="line">docker tag docker.io/mirrorgooglecontainers/kube-controller-manager-amd64:v1.13.0 k8s.gcr.io/kube-controller-manager:v1.13.0</span><br><span class="line">docker tag docker.io/mirrorgooglecontainers/kube-scheduler-amd64:v1.13.0 k8s.gcr.io/kube-scheduler:v1.13.0</span><br><span class="line">docker tag docker.io/mirrorgooglecontainers/kube-proxy-amd64:v1.13.0 k8s.gcr.io/kube-proxy:v1.13.0</span><br><span class="line">docker tag docker.io/mirrorgooglecontainers/pause:3.1  k8s.gcr.io/pause:3.1</span><br><span class="line">docker tag docker.io/mirrorgooglecontainers/etcd-amd64:3.2.24  k8s.gcr.io/etcd:3.2.24</span><br><span class="line">docker tag docker.io/coredns/coredns:1.2.6  k8s.gcr.io/coredns:1.2.6</span><br><span class="line"><span class="comment"># 删除多余镜像</span></span><br><span class="line">docker rmi mirrorgooglecontainers/kube-apiserver-amd64:v1.13.0</span><br><span class="line">docker rmi mirrorgooglecontainers/kube-controller-manager-amd64:v1.13.0</span><br><span class="line">docker rmi mirrorgooglecontainers/kube-scheduler-amd64:v1.13.0</span><br><span class="line">docker rmi mirrorgooglecontainers/kube-proxy-amd64:v1.13.0</span><br><span class="line">docker rmi mirrorgooglecontainers/pause:3.1</span><br><span class="line">docker rmi mirrorgooglecontainers/etcd-amd64:3.2.24</span><br><span class="line">docker rmi coredns/coredns:1.2.6</span><br></pre></td></tr></table></figure><p>再次运行init</p></blockquote><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">[init] Using Kubernetes version: v1.13.0</span><br><span class="line">[preflight] Running pre-flight checks</span><br><span class="line">   [WARNING Swap]: running with swap on is not supported. Please <span class="built_in">disable</span> swap</span><br><span class="line">   [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.09.0. Latest validated version: 18.06</span><br><span class="line">[preflight] Pulling images required <span class="keyword">for</span> setting up a Kubernetes cluster</span><br><span class="line">[preflight] This might take a minute or two, depending on the speed of your internet connection</span><br><span class="line">[preflight] You can also perform this action <span class="keyword">in</span> beforehand using <span class="string">&#x27;kubeadm config images pull&#x27;</span></span><br><span class="line">[kubelet-start] Writing kubelet environment file with flags to file <span class="string">&quot;/var/lib/kubelet/kubeadm-flags.env&quot;</span></span><br><span class="line">[kubelet-start] Writing kubelet configuration to file <span class="string">&quot;/var/lib/kubelet/config.yaml&quot;</span></span><br><span class="line">[kubelet-start] Activating the kubelet service</span><br><span class="line">[certs] Using certificateDir folder <span class="string">&quot;/etc/kubernetes/pki&quot;</span></span><br><span class="line">[certs] Generating <span class="string">&quot;ca&quot;</span> certificate and key</span><br><span class="line">[certs] Generating <span class="string">&quot;apiserver&quot;</span> certificate and key</span><br><span class="line">[certs] apiserver serving cert is signed <span class="keyword">for</span> DNS names [master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.0.121]</span><br><span class="line">[certs] Generating <span class="string">&quot;apiserver-kubelet-client&quot;</span> certificate and key</span><br><span class="line">[certs] Generating <span class="string">&quot;front-proxy-ca&quot;</span> certificate and key</span><br><span class="line">[certs] Generating <span class="string">&quot;front-proxy-client&quot;</span> certificate and key</span><br><span class="line">[certs] Generating <span class="string">&quot;etcd/ca&quot;</span> certificate and key</span><br><span class="line">[certs] Generating <span class="string">&quot;etcd/peer&quot;</span> certificate and key</span><br><span class="line">[certs] etcd/peer serving cert is signed <span class="keyword">for</span> DNS names [master localhost] and IPs [192.168.0.121 127.0.0.1 ::1]</span><br><span class="line">[certs] Generating <span class="string">&quot;etcd/healthcheck-client&quot;</span> certificate and key</span><br><span class="line">[certs] Generating <span class="string">&quot;etcd/server&quot;</span> certificate and key</span><br><span class="line">[certs] etcd/server serving cert is signed <span class="keyword">for</span> DNS names [master localhost] and IPs [192.168.0.121 127.0.0.1 ::1]</span><br><span class="line">[certs] Generating <span class="string">&quot;apiserver-etcd-client&quot;</span> certificate and key</span><br><span class="line">[certs] Generating <span class="string">&quot;sa&quot;</span> key and public key</span><br><span class="line">[kubeconfig] Using kubeconfig folder <span class="string">&quot;/etc/kubernetes&quot;</span></span><br><span class="line">[kubeconfig] Writing <span class="string">&quot;admin.conf&quot;</span> kubeconfig file</span><br><span class="line">[kubeconfig] Writing <span class="string">&quot;kubelet.conf&quot;</span> kubeconfig file</span><br><span class="line">[kubeconfig] Writing <span class="string">&quot;controller-manager.conf&quot;</span> kubeconfig file</span><br><span class="line">[kubeconfig] Writing <span class="string">&quot;scheduler.conf&quot;</span> kubeconfig file</span><br><span class="line">[control-plane] Using manifest folder <span class="string">&quot;/etc/kubernetes/manifests&quot;</span></span><br><span class="line">[control-plane] Creating static Pod manifest <span class="keyword">for</span> <span class="string">&quot;kube-apiserver&quot;</span></span><br><span class="line">[control-plane] Creating static Pod manifest <span class="keyword">for</span> <span class="string">&quot;kube-controller-manager&quot;</span></span><br><span class="line">[control-plane] Creating static Pod manifest <span class="keyword">for</span> <span class="string">&quot;kube-scheduler&quot;</span></span><br><span class="line">[etcd] Creating static Pod manifest <span class="keyword">for</span> <span class="built_in">local</span> etcd <span class="keyword">in</span> <span class="string">&quot;/etc/kubernetes/manifests&quot;</span></span><br><span class="line">[wait-control-plane] Waiting <span class="keyword">for</span> the kubelet to boot up the control plane as static Pods from directory <span class="string">&quot;/etc/kubernetes/manifests&quot;</span>. This can take up to 4m0s</span><br><span class="line">[apiclient] All control plane components are healthy after 28.005180 seconds</span><br><span class="line">[uploadconfig] storing the configuration used <span class="keyword">in</span> ConfigMap <span class="string">&quot;kubeadm-config&quot;</span> <span class="keyword">in</span> the <span class="string">&quot;kube-system&quot;</span> Namespace</span><br><span class="line">[kubelet] Creating a ConfigMap <span class="string">&quot;kubelet-config-1.13&quot;</span> <span class="keyword">in</span> namespace kube-system with the configuration <span class="keyword">for</span> the kubelets <span class="keyword">in</span> the cluster</span><br><span class="line">[patchnode] Uploading the CRI Socket information <span class="string">&quot;/var/run/dockershim.sock&quot;</span> to the Node API object <span class="string">&quot;master&quot;</span> as an annotation</span><br><span class="line">[mark-control-plane] Marking the node master as control-plane by adding the label <span class="string">&quot;node-role.kubernetes.io/master=&#x27;&#x27;&quot;</span></span><br><span class="line">[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]</span><br><span class="line">[bootstrap-token] Using token: bag6qi.1qc7mfs75d5r1znq</span><br><span class="line">[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles</span><br><span class="line">[bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs <span class="keyword">in</span> order <span class="keyword">for</span> nodes to get long term certificate credentials</span><br><span class="line">[bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token</span><br><span class="line">[bootstraptoken] configured RBAC rules to allow certificate rotation <span class="keyword">for</span> all node client certificates <span class="keyword">in</span> the cluster</span><br><span class="line">[bootstraptoken] creating the <span class="string">&quot;cluster-info&quot;</span> ConfigMap <span class="keyword">in</span> the <span class="string">&quot;kube-public&quot;</span> namespace</span><br><span class="line">[addons] Applied essential addon: CoreDNS</span><br><span class="line">[addons] Applied essential addon: kube-proxy</span><br><span class="line"></span><br><span class="line">Your Kubernetes master has initialized successfully!</span><br><span class="line"></span><br><span class="line">To start using your cluster, you need to run the following as a regular user:</span><br><span class="line"></span><br><span class="line">  mkdir -p <span class="variable">$HOME</span>/.kube</span><br><span class="line">  sudo cp -i /etc/kubernetes/admin.conf <span class="variable">$HOME</span>/.kube/config</span><br><span class="line">  sudo chown $(id -u):$(id -g) <span class="variable">$HOME</span>/.kube/config</span><br><span class="line"></span><br><span class="line">You should now deploy a pod network to the cluster.</span><br><span class="line">Run <span class="string">&quot;kubectl apply -f [podnetwork].yaml&quot;</span> with one of the options listed at:</span><br><span class="line">  https://kubernetes.io/docs/concepts/cluster-administration/addons/</span><br><span class="line"></span><br><span class="line">You can now join any number of machines by running the following on each node</span><br><span class="line">as root:</span><br><span class="line"></span><br><span class="line">  kubeadm join 192.168.0.121:6443 --token bag6qi.1qc7mfs75d5r1znq --discovery-token-ca-cert-hash sha256:5c3533240bf14b1aacc33e92ebde929f9e573752b629262fa43db02c4e656b8f</span><br></pre></td></tr></table></figure><blockquote><p>上面记录了完成的初始化输出的内容，根据输出的内容基本上可以看出手动初始化安装一个Kubernetes集群所需要的关键步骤。<br>其中有以下关键内容：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[kubelet-start] 生成kubelet的配置文件/var/lib/kubelet/config.yaml</span><br><span class="line">[certificates]生成相关的各种证书</span><br><span class="line">[kubeconfig]生成相关的kubeconfig文件</span><br><span class="line">[bootstraptoken]生成token记录下来，后边使用kubeadm join往集群中添加节点时会用到</span><br></pre></td></tr></table></figure><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">下面的命令是配置常规用户如何使用kubectl访问集群：    </span><br><span class="line">```bash</span><br><span class="line">mkdir -p <span class="variable">$HOME</span>/.kube</span><br><span class="line">sudo cp -i /etc/kubernetes/admin.conf <span class="variable">$HOME</span>/.kube/config</span><br><span class="line">sudo chown $(id -u):$(id -g) <span class="variable">$HOME</span>/.kube/config</span><br></pre></td></tr></table></figure><p>最后给出了将节点加入集群的命令<code>kubeadm join 192.168.0.121:6443 --token bag6qi.1qc7mfs75d5r1znq --discovery-token-ca-cert-hash sha256:5c3533240bf14b1aacc33e92ebde929f9e573752b629262fa43db02c4e656b8f</code></p></blockquote><p>查看一下集群状态：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get cs</span><br></pre></td></tr></table></figure><p>确认个组件都处于healthy状态。</p><p>集群初始化如果遇到问题，可以使用下面的命令进行清理：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">kubeadm reset</span><br><span class="line">ifconfig cni0 down</span><br><span class="line">ip link delete cni0</span><br><span class="line">ifconfig flannel.1 down</span><br><span class="line">ip link delete flannel.1</span><br><span class="line">rm -rf /var/lib/cni/</span><br></pre></td></tr></table></figure><h3 id="安装Pod-Network"><a href="#安装Pod-Network" class="headerlink" title="安装Pod Network"></a>安装Pod Network</h3><p>接下来安装flannel network add-on：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">kubectl create -f  https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml</span><br><span class="line">kubectl apply -f  https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml</span><br></pre></td></tr></table></figure><p>执行结果    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">clusterrole.rbac.authorization.k8s.io/flannel created</span><br><span class="line">clusterrolebinding.rbac.authorization.k8s.io/flannel created</span><br><span class="line">serviceaccount/flannel created</span><br><span class="line">configmap/kube-flannel-cfg created</span><br><span class="line">daemonset.extensions/kube-flannel-ds-amd64 created</span><br><span class="line">daemonset.extensions/kube-flannel-ds-arm64 created</span><br><span class="line">daemonset.extensions/kube-flannel-ds-arm created</span><br><span class="line">daemonset.extensions/kube-flannel-ds-ppc64le created</span><br><span class="line">daemonset.extensions/kube-flannel-ds-s390x created</span><br></pre></td></tr></table></figure><blockquote><p>这里注意kube-flannel.yml这个文件里的flannel的镜像是0.10.0，quay.io/coreos/flannel:v0.10.0-amd64</p></blockquote><p>如果Node有多个网卡的话，参考<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2t1YmVybmV0ZXMva3ViZXJuZXRlcy9pc3N1ZXMvMzk3MDE=">flannel issues 39701<i class="fa fa-external-link-alt"></i></span>，目前需要在<code>kube-flannel.yml</code>中使用–iface参数指定集群主机内网网卡的名称，否则可能会出现dns无法解析。需要将<code>kube-flannel.yml</code>下载到本地，flanneld启动参数加上<code>–iface=&lt;iface-name&gt;</code>    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line">containers:</span><br><span class="line">      - name: kube-flannel</span><br><span class="line">        image: quay.io/coreos/flannel:v0.10.0-amd64</span><br><span class="line">        <span class="built_in">command</span>:</span><br><span class="line">        - /opt/bin/flanneld</span><br><span class="line">        args:</span><br><span class="line">        - --ip-masq</span><br><span class="line">        - --kube-subnet-mgr</span><br><span class="line">        - --iface=eth1</span><br><span class="line">......</span><br></pre></td></tr></table></figure><p>使用<code>kubectl get pod --all-namespaces -o wide</code>确保所有的Pod都处于Running状态。    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">NAMESPACE     NAME                             READY   STATUS    RESTARTS   AGE   IP              NODE     NOMINATED NODE   READINESS GATES</span><br><span class="line">default       curl-66959f6557-47582            1/1     Running   2          36h   10.244.0.6      master   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   coredns-86c58d9df4-wdh4x         1/1     Running   1          37h   10.244.0.5      master   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   coredns-86c58d9df4-zfrm5         1/1     Running   1          37h   10.244.0.7      master   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   etcd-master                      1/1     Running   2          37h   192.168.0.121   master   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   kube-apiserver-master            1/1     Running   2          37h   192.168.0.121   master   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   kube-controller-manager-master   1/1     Running   3          37h   192.168.0.121   master   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   kube-flannel-ds-amd64-66kns      1/1     Running   1          36h   192.168.0.123   node2    &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   kube-flannel-ds-amd64-6s7v5      1/1     Running   1          37h   192.168.0.121   master   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   kube-flannel-ds-amd64-9j5sj      1/1     Running   1          36h   192.168.0.122   node1    &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   kube-proxy-4vn59                 1/1     Running   1          36h   192.168.0.123   node2    &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   kube-proxy-fsp2d                 1/1     Running   1          36h   192.168.0.121   master   &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   kube-proxy-qkg7t                 1/1     Running   1          36h   192.168.0.122   node1    &lt;none&gt;           &lt;none&gt;</span><br><span class="line">kube-system   kube-scheduler-master            1/1     Running   3          37h   192.168.0.121   master   &lt;none&gt;           &lt;none&gt;</span><br></pre></td></tr></table></figure><h3 id="master-node参与工作负载"><a href="#master-node参与工作负载" class="headerlink" title="master node参与工作负载"></a>master node参与工作负载</h3><p>使用kubeadm初始化的集群，出于安全考虑Pod不会被调度到Master Node上，也就是说Master Node不参与工作负载。这是因为当前的master节点master被打上了<code>node-role.kubernetes.io/master:NoSchedule</code>的污点：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">kubectl describe node master | grep Taint</span><br><span class="line">Taints:             node-role.kubernetes.io/master:NoSchedule</span><br></pre></td></tr></table></figure><p>因为这里搭建的是测试环境，去掉这个污点使master参与工作负载：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">kubectl taint nodes master node-role.kubernetes.io/master-</span><br><span class="line">node <span class="string">&quot;master&quot;</span> untainted</span><br></pre></td></tr></table></figure><h3 id="测试DNS"><a href="#测试DNS" class="headerlink" title="测试DNS"></a>测试DNS</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl run curl --image=radial/busyboxplus:curl -it</span><br></pre></td></tr></table></figure><p>该步骤时间较长，请耐心等待<br>当出现    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[ root@curl-66959f6557-47582:/ ]$</span><br></pre></td></tr></table></figure><p>时，执行    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nslookup kubernetes.default</span><br></pre></td></tr></table></figure><p>结果为    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Server:    10.96.0.10</span><br><span class="line">Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local</span><br><span class="line"></span><br><span class="line">Name:      kubernetes.default</span><br><span class="line">Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local</span><br></pre></td></tr></table></figure><h3 id="向Kubernetes集群中添加Node节点"><a href="#向Kubernetes集群中添加Node节点" class="headerlink" title="向Kubernetes集群中添加Node节点"></a>向Kubernetes集群中添加Node节点</h3><p>下面我们将node2这个主机添加到Kubernetes集群中，因为我们同样在node2上的kubelet的启动参数中去掉了必须关闭swap的限制，所以同样需要–ignore-preflight-errors=Swap这个参数。 在node2上执行:    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">kubeadm \</span><br><span class="line">    join 192.168.0.121:6443 \</span><br><span class="line">    --token bag6qi.1qc7mfs75d5r1znq \</span><br><span class="line">    --discovery-token-ca-cert-hash sha256:5c3533240bf14b1aacc33e92ebde929f9e573752b629262fa43db02c4e656b8f \</span><br><span class="line">    --ignore-preflight-errors=Swap</span><br></pre></td></tr></table></figure><p>结果    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">[preflight] Running pre-flight checks</span><br><span class="line">   [WARNING Swap]: running with swap on is not supported. Please <span class="built_in">disable</span> swap</span><br><span class="line">   [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.09.0. Latest validated version: 18.06</span><br><span class="line">[discovery] Trying to connect to API Server <span class="string">&quot;192.168.0.121:6443&quot;</span></span><br><span class="line">[discovery] Created cluster-info discovery client, requesting info from <span class="string">&quot;https://192.168.0.121:6443&quot;</span></span><br><span class="line">[discovery] Requesting info from <span class="string">&quot;https://192.168.0.121:6443&quot;</span> again to validate TLS against the pinned public key</span><br><span class="line">[discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server <span class="string">&quot;192.168.0.121:6443&quot;</span></span><br><span class="line">[discovery] Successfully established connection with API Server <span class="string">&quot;192.168.0.121:6443&quot;</span></span><br><span class="line">[join] Reading configuration from the cluster...</span><br><span class="line">[join] FYI: You can look at this config file with <span class="string">&#x27;kubectl -n kube-system get cm kubeadm-config -oyaml&#x27;</span></span><br><span class="line">[kubelet] Downloading configuration <span class="keyword">for</span> the kubelet from the <span class="string">&quot;kubelet-config-1.13&quot;</span> ConfigMap <span class="keyword">in</span> the kube-system namespace</span><br><span class="line">[kubelet-start] Writing kubelet configuration to file <span class="string">&quot;/var/lib/kubelet/config.yaml&quot;</span></span><br><span class="line">[kubelet-start] Writing kubelet environment file with flags to file <span class="string">&quot;/var/lib/kubelet/kubeadm-flags.env&quot;</span></span><br><span class="line">[kubelet-start] Activating the kubelet service</span><br><span class="line">[tlsbootstrap] Waiting <span class="keyword">for</span> the kubelet to perform the TLS Bootstrap...</span><br><span class="line">[patchnode] Uploading the CRI Socket information <span class="string">&quot;/var/run/dockershim.sock&quot;</span> to the Node API object <span class="string">&quot;node1&quot;</span> as an annotation</span><br><span class="line"></span><br><span class="line">This node has joined the cluster:</span><br><span class="line">* Certificate signing request was sent to apiserver and a response was received.</span><br><span class="line">* The Kubelet was informed of the new secure connection details.</span><br><span class="line"></span><br><span class="line">Run <span class="string">&#x27;kubectl get nodes&#x27;</span> on the master to see this node join the cluster.</span><br></pre></td></tr></table></figure><p>加入集群成功，在master节点执行<code>kubectl get nodes</code>结果    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">NAME     STATUS   ROLES    AGE   VERSION</span><br><span class="line">master   Ready    master   36h   v1.13.2</span><br><span class="line">node1    Ready    &lt;none&gt;   35h   v1.13.2</span><br><span class="line">node2    Ready    &lt;none&gt;   35h   v1.13.2</span><br></pre></td></tr></table></figure><p>如何从集群中移除Node<br>如果需要从集群中移除node2这个Node执行下面的命令：<br>在master节点上执行：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">kubectl drain node1 --delete-local-data --force --ignore-daemonsets</span><br><span class="line">kubectl delete node node1</span><br></pre></td></tr></table></figure><p>在node1上执行：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">kubeadm reset</span><br><span class="line">ifconfig cni0 down</span><br><span class="line">ip link delete cni0</span><br><span class="line">ifconfig flannel.1 down</span><br><span class="line">ip link delete flannel.1</span><br><span class="line">rm -rf /var/lib/cni/</span><br></pre></td></tr></table></figure><p>在master上执行：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl delete node node1</span><br></pre></td></tr></table></figure><h3 id="kube-proxy开启ipvs"><a href="#kube-proxy开启ipvs" class="headerlink" title="kube-proxy开启ipvs"></a>kube-proxy开启ipvs</h3><p>修改<code>ConfigMap</code>的<code>kube-system/kube-proxy</code>中的<code>config.conf</code>，<code>mode: &quot;ipvs&quot;</code>：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl edit cm kube-proxy -n kube-system</span><br></pre></td></tr></table></figure><p>之后重启各个节点上的kube-proxy pod，在master节点运行：    </p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pod -n kube-system | grep kube-proxy | awk <span class="string">&#x27;&#123;system(&quot;kubectl delete pod &quot;$1&quot; -n kube-system&quot;)&#125;&#x27;</span></span><br></pre></td></tr></table></figure><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pod -n kube-system | grep kube-proxy</span><br><span class="line"></span><br><span class="line"><span class="comment"># 结果</span></span><br><span class="line">kube-proxy-4vn59                 1/1     Running   0          100s</span><br><span class="line">kube-proxy-fsp2d                 1/1     Running   0          86s</span><br><span class="line">kube-proxy-qkg7t                 1/1     Running   0          97s</span><br><span class="line"></span><br><span class="line">kubectl logs kube-proxy-4vn59 -n kube-system</span><br><span class="line"></span><br><span class="line"><span class="comment"># 结果</span></span><br><span class="line">I0115 08:53:45.580868       1 server_others.go:189] Using ipvs Proxier.</span><br><span class="line">W0115 08:53:45.581086       1 proxier.go:365] IPVS scheduler not specified, use rr by default</span><br><span class="line">I0115 08:53:45.583075       1 server_others.go:216] Tearing down inactive rules.</span><br><span class="line">I0115 08:53:45.614150       1 server.go:464] Version: v1.13.0</span><br><span class="line">I0115 08:53:45.618996       1 conntrack.go:52] Setting nf_conntrack_max to 131072</span><br><span class="line">I0115 08:53:45.619467       1 config.go:202] Starting service config controller</span><br><span class="line">I0115 08:53:45.619474       1 controller_utils.go:1027] Waiting <span class="keyword">for</span> caches to sync <span class="keyword">for</span> service config controller</span><br><span class="line">I0115 08:53:45.619482       1 config.go:102] Starting endpoints config controller</span><br><span class="line">I0115 08:53:45.619485       1 controller_utils.go:1027] Waiting <span class="keyword">for</span> caches to sync <span class="keyword">for</span> endpoints config controller</span><br><span class="line">I0115 08:53:45.719664       1 controller_utils.go:1034] Caches are synced <span class="keyword">for</span> service config controller</span><br><span class="line">I0115 08:53:45.719739       1 controller_utils.go:1034] Caches are synced <span class="keyword">for</span> endpoints config controller</span><br><span class="line"><span class="comment"># 结果</span></span><br></pre></td></tr></table></figure><p>日志中打印出了Using ipvs Proxier，说明ipvs模式已经开启。</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;kubeadm是Kubernetes官方提供的用于快速安装Kubernetes集群的工具，伴随Kubernetes每个版本的发布都会同步更新，kubeadm会对集群配置方面的一些实践做调整，通过实验kubeadm可以学习到Kubernetes官方在集群配置上一些新的最佳实践。&lt;/p&gt;</summary>
    
    
    
    <category term="安装" scheme="https://www.kiilin.com/categories/%E5%AE%89%E8%A3%85/"/>
    
    <category term="install" scheme="https://www.kiilin.com/categories/%E5%AE%89%E8%A3%85/install/"/>
    
    
    <category term="安装" scheme="https://www.kiilin.com/tags/%E5%AE%89%E8%A3%85/"/>
    
    <category term="install" scheme="https://www.kiilin.com/tags/install/"/>
    
    <category term="docker" scheme="https://www.kiilin.com/tags/docker/"/>
    
    <category term="k8s" scheme="https://www.kiilin.com/tags/k8s/"/>
    
    <category term="kubernetes" scheme="https://www.kiilin.com/tags/kubernetes/"/>
    
  </entry>
  
  <entry>
    <title>windows下安装mysql8.0</title>
    <link href="https://www.kiilin.com/blog/mysql8_0_install.html"/>
    <id>https://www.kiilin.com/blog/mysql8_0_install.html</id>
    <published>2018-10-14T11:57:00.000Z</published>
    <updated>2020-12-31T02:06:41.610Z</updated>
    
    <content type="html"><![CDATA[<p>MySQL 8.0 正式版 8.0.12 已发布，官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍，还带来了大量的改进和更快的性能！</p><a id="more"></a><h3 id="一、-下载"><a href="#一、-下载" class="headerlink" title="一、 下载"></a>一、 下载</h3><p>下载地址 <span class="exturl" data-url="aHR0cHM6Ly93d3cubXlzcWwuY29tLw==">mysql.com<i class="fa fa-external-link-alt"></i></span></p><ul><li><h4 id="进入下载界面"><a href="#进入下载界面" class="headerlink" title="进入下载界面"></a>进入下载界面</h4><p><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/mysql_download_1.png" alt="进入下载界面"></p></li><li><h4 id="选择社区版下载"><a href="#选择社区版下载" class="headerlink" title="选择社区版下载"></a>选择社区版下载</h4><p><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/mysql_download_2.png" alt="选择社区版下载"></p></li><li><h4 id="选择下载服务"><a href="#选择下载服务" class="headerlink" title="选择下载服务"></a>选择下载服务</h4><p><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/mysql_download_4.png" alt="选择下载服务"></p></li><li><h4 id="选择系统"><a href="#选择系统" class="headerlink" title="选择系统"></a>选择系统</h4><p><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/mysql_download_5.png" alt="选择系统"></p></li><li><h4 id="下载"><a href="#下载" class="headerlink" title="下载"></a>下载</h4><p><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/mysql_download_6.png" alt="下载"></p></li></ul><h3 id="二、-安装"><a href="#二、-安装" class="headerlink" title="二、 安装"></a>二、 安装</h3><ul><li><h4 id="解压"><a href="#解压" class="headerlink" title="解压"></a>解压</h4></li></ul><p>将下载好的zip包解压到安装目录。教程以 <code>E:\Program Files\mysql-8.0.11-winx64</code> 为例</p><ul><li><h4 id="配置文件"><a href="#配置文件" class="headerlink" title="配置文件"></a>配置文件</h4></li></ul><p>在Windows系统中，配置文件默认是安装目录下的 my.ini 文件，部分配置需要在初始安装时配置，大部分也可以在安装完成后进行更改。<br>在安装根目录下添加 <code>my.ini</code><br>写入基本配置</p><figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">[mysqld]</span></span><br><span class="line"><span class="attr">sql_mode</span>=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES</span><br><span class="line"><span class="comment"># 设置3306端口</span></span><br><span class="line"><span class="attr">port</span>=<span class="number">3306</span></span><br><span class="line"><span class="comment"># 设置mysql的安装目录</span></span><br><span class="line"><span class="comment"># 切记此处一定要用双斜杠\\，单斜杠我这里会出错，不过看别人的教程，有的是单斜杠。自己尝试吧</span></span><br><span class="line"><span class="attr">basedir</span>=E:\\Program Files\\mysql-<span class="number">8.0</span>.<span class="number">11</span>-winx64   </span><br><span class="line"><span class="comment"># 设置mysql数据库的数据的存放目录</span></span><br><span class="line"><span class="comment"># 此处同上</span></span><br><span class="line"><span class="attr">datadir</span>=E:\\Program Files\\mysql-<span class="number">8.0</span>.<span class="number">11</span>-winx64\\Data  </span><br><span class="line"><span class="comment"># 允许最大连接数</span></span><br><span class="line"><span class="attr">max_connections</span>=<span class="number">200</span></span><br><span class="line"><span class="comment"># 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统</span></span><br><span class="line"><span class="attr">max_connect_errors</span>=<span class="number">10</span></span><br><span class="line"><span class="comment"># 服务端使用的字符集默认为UTF8</span></span><br><span class="line"><span class="attr">character-set-server</span>=utf8</span><br><span class="line"><span class="comment"># 创建新表时将使用的默认存储引擎</span></span><br><span class="line"><span class="attr">default-storage-engine</span>=INNODB</span><br><span class="line"><span class="comment"># 默认使用“mysql_native_password”插件认证</span></span><br><span class="line"><span class="attr">default_authentication_plugin</span>=mysql_native_password</span><br><span class="line"><span class="section">[mysql]</span></span><br><span class="line"><span class="comment"># 设置mysql客户端默认字符集</span></span><br><span class="line"><span class="attr">default-character-set</span>=utf8</span><br><span class="line"><span class="section">[client]</span></span><br><span class="line"><span class="comment"># 设置mysql客户端连接服务端时默认使用的端口</span></span><br><span class="line"><span class="attr">port</span>=<span class="number">3306</span></span><br><span class="line"><span class="attr">default-character-set</span>=utf8</span><br></pre></td></tr></table></figure><p>这里的<code>basedir</code>是我本地的安装路径，<code>datadir</code>是数据保存路径，可根据需要自行修改。<br>其他配置信息可参考 <span class="exturl" data-url="aHR0cHM6Ly9kZXYubXlzcWwuY29tL2RvYy9yZWZtYW4vOC4wL2VuL3NlcnZlci1vcHRpb24tdmFyaWFibGUtcmVmZXJlbmNlLmh0bWw=">mysql官方配置<i class="fa fa-external-link-alt"></i></span></p><ul><li><h4 id="初始化数据库"><a href="#初始化数据库" class="headerlink" title="初始化数据库"></a>初始化数据库</h4></li></ul><p>在安装路径下的<code>bin</code>目录下执行(也可配置环境变量)</p><figure class="highlight brainfuck"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">mysqld</span> --<span class="comment">install</span> <span class="title">[</span><span class="comment">服务名</span><span class="title">]</span> --<span class="comment">console</span></span><br></pre></td></tr></table></figure><p>后面的服务名可以不写，默认的名字为mysql。如果你的电脑上需要安装多个MySQL服务，就可以用不同的名字区分了，比如 mysql57 和 mysql8。</p><p>继续执行</p><figure class="highlight brainfuck"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">mysqld</span> --<span class="comment">initialize</span> --<span class="comment">console</span></span><br></pre></td></tr></table></figure><p>命令查看默认密码<br><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/mysqld_install_pwd.png" alt="默认密码"></p><p>安装完成之后，就可以通过命令<code>net start mysql</code>启动MySQL的服务了。<br>参考 <span class="exturl" data-url="aHR0cHM6Ly9kZXYubXlzcWwuY29tL2RvYy9yZWZtYW4vOC4wL2VuL3dpbmRvd3Mtc3RhcnQtc2VydmljZS5odG1s">mysql官方文档<i class="fa fa-external-link-alt"></i></span></p><p>如果出现以下错误<br><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/mysqld_install_error.png" alt="安装错误"><br>则需要使用管理员启动cmd</p><p>如果不小心关闭窗口未记录默认密码，则需要将data文件夹删除后重复第三步即可。</p><ul><li><h4 id="修改密码"><a href="#修改密码" class="headerlink" title="修改密码"></a>修改密码</h4></li></ul><p>上述工作完成后即可登录mysql服务修改密码</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -uroot -p[<span class="built_in">pwd</span>]</span><br></pre></td></tr></table></figure><p><code>[pwd]</code>为第三步记录的密码。当然也可以<code>mysql -utoot -p</code>后再输入密码<br>执行</p><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="builtin-name">SET</span> <span class="attribute">PASSWORD</span>=<span class="string">&#x27;[newPwd]&#x27;</span>;</span><br></pre></td></tr></table></figure><p><code>[newPwd]</code>为需要设置的新密码</p><p><code>8.0.4</code>之后的版本，如果在<code>my.ini</code>中未配置</p><figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">default_authentication_plugin</span>=mysql_native_password</span><br></pre></td></tr></table></figure><p>这种方式无法修改密码。<br>因为<code>8.0.4</code>之前，MySQL的密码认证插件是<code>mysql_native_password</code>，而<code>8.0.4</code>之后使用的是<code>caching_sha2_password</code>。<br>需要执行</p><figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">USER</span> <span class="string">&#x27;root&#x27;</span>@<span class="string">&#x27;localhost&#x27;</span> IDENTIFIED <span class="keyword">WITH</span> mysql_native_password <span class="keyword">BY</span> <span class="string">&#x27;password&#x27;</span>;</span><br><span class="line">flush <span class="keyword">privileges</span>;</span><br></pre></td></tr></table></figure><p>修改密码验证插件，同时修改密码。<br>参考 <span class="exturl" data-url="aHR0cHM6Ly9kZXYubXlzcWwuY29tL2RvYy9yZWZtYW4vOC4wL2VuL3VwZ3JhZGluZy1mcm9tLXByZXZpb3VzLXNlcmllcy5odG1sI3VwZ3JhZGUtY2FjaGluZy1zaGEyLXBhc3N3b3Jk">mysql官方文档<i class="fa fa-external-link-alt"></i></span></p><p>至此，windows下安装mysql8.0就完成了。</p>]]></content>
    
    
    <summary type="html">&lt;p&gt;MySQL 8.0 正式版 8.0.12 已发布，官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍，还带来了大量的改进和更快的性能！&lt;/p&gt;</summary>
    
    
    
    <category term="安装" scheme="https://www.kiilin.com/categories/%E5%AE%89%E8%A3%85/"/>
    
    
    <category term="windows" scheme="https://www.kiilin.com/tags/windows/"/>
    
    <category term="mysql8" scheme="https://www.kiilin.com/tags/mysql8/"/>
    
    <category term="mysql8.0" scheme="https://www.kiilin.com/tags/mysql8-0/"/>
    
    <category term="安装" scheme="https://www.kiilin.com/tags/%E5%AE%89%E8%A3%85/"/>
    
    <category term="install" scheme="https://www.kiilin.com/tags/install/"/>
    
  </entry>
  
  <entry>
    <title>jquery 自定义模板（jquery-template）</title>
    <link href="https://www.kiilin.com/blog/template.html"/>
    <id>https://www.kiilin.com/blog/template.html</id>
    <published>2018-08-24T07:13:00.000Z</published>
    <updated>2020-12-31T02:06:41.610Z</updated>
    
    <content type="html"><![CDATA[<h1 id="jquery-template"><a href="#jquery-template" class="headerlink" title="jquery-template"></a>jquery-template</h1><h4 id="项目介绍"><a href="#项目介绍" class="headerlink" title="项目介绍"></a>项目介绍</h4><p>利用模板及正则解决js拼接html代码问题</p><p><a target="_blank" href="http://www.kiilin.com/jquery-template/">演示地址</a> | <span class="exturl" data-url="aHR0cHM6Ly9naXRlZS5jb20va2lpbGluL2pxdWVyeS10ZW1wbGF0ZQ==">bug提交地址<i class="fa fa-external-link-alt"></i></span></p><a id="more"></a><h4 id="目的"><a href="#目的" class="headerlink" title="目的"></a>目的</h4><p>插件主要目的：解决js拼接html代码</p><h4 id="参数说明"><a href="#参数说明" class="headerlink" title="参数说明"></a>参数说明</h4><table><thead><tr><th align="left">参数名</th><th align="center">是否必须</th><th align="left">描述</th><th align="left">默认值</th></tr></thead><tbody><tr><td align="left">template</td><td align="center">必须</td><td align="left">模板的jquery对象</td><td align="left">null</td></tr><tr><td align="left">target</td><td align="center">可选</td><td align="left">被刷新的dom的jquery对象</td><td align="left">null</td></tr><tr><td align="left">data</td><td align="center">必须</td><td align="left">ajax请求后返回的数据，也就是需要替换到模板占位符的数据。</td><td align="left">null</td></tr><tr><td align="left">nullText</td><td align="center">可选</td><td align="left">当占位符对应的数据为null或不存在时的默认显示，可以是字符串，也可以是html</td><td align="left">‘’</td></tr><tr><td align="left">prefix</td><td align="center">可选</td><td align="left">占位符前缀</td><td align="left">‘[‘</td></tr><tr><td align="left">suffix</td><td align="center">可选</td><td align="left">占位符后缀</td><td align="left">‘]’</td></tr></tbody></table><h4 id="使用说明"><a href="#使用说明" class="headerlink" title="使用说明"></a>使用说明</h4><h5 id="v1-0-1-版本仅提供两个功能"><a href="#v1-0-1-版本仅提供两个功能" class="headerlink" title="v1.0.1 版本仅提供两个功能"></a>v1.0.1 版本仅提供两个功能</h5><h6 id="ajax请求后刷新部分dom"><a href="#ajax请求后刷新部分dom" class="headerlink" title="ajax请求后刷新部分dom"></a>ajax请求后刷新部分dom</h6><blockquote><p>定制模板</p></blockquote><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/html&quot;</span> <span class="attr">id</span>=<span class="string">&quot;userInfoTemp&quot;</span>&gt;</span></span><br><span class="line"><span class="handlebars"><span class="xml">  <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span></span></span><br><span class="line"><span class="handlebars"><span class="xml">    <span class="tag">&lt;<span class="name">td</span>&gt;</span>[id]<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span></span><br><span class="line"><span class="handlebars"><span class="xml">    <span class="tag">&lt;<span class="name">td</span>&gt;</span>[name]<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span></span><br><span class="line"><span class="handlebars"><span class="xml">    <span class="tag">&lt;<span class="name">td</span>&gt;</span>[sex]<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span></span><br><span class="line"><span class="handlebars"><span class="xml">    <span class="tag">&lt;<span class="name">td</span>&gt;</span>[age]<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span></span><br><span class="line"><span class="handlebars"><span class="xml">  <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span></span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure><blockquote><p>调用模板</p></blockquote><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$</span>.template(&#123;</span><br><span class="line">  template: <span class="variable">$</span>(<span class="string">&quot;#userInfoTemp&quot;</span>),</span><br><span class="line">  target: <span class="variable">$</span>(<span class="string">&quot;#userInfo&quot;</span>),</span><br><span class="line">  <span class="keyword">data</span>: _result.data,</span><br><span class="line">  nullText: <span class="string">&#x27;&lt;span style=&quot;color: red;&quot;&gt;暂无&lt;/span&gt;&#x27;</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><h6 id="datatables等框架自定义列等场景"><a href="#datatables等框架自定义列等场景" class="headerlink" title="datatables等框架自定义列等场景"></a>datatables等框架自定义列等场景</h6><blockquote><p>定制模板</p></blockquote><figure class="highlight angelscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=<span class="string">&quot;text/html&quot;</span> id=<span class="string">&quot;actionsTemp&quot;</span>&gt;</span><br><span class="line">  &lt;div <span class="keyword">class</span>=&quot;<span class="symbol">btns</span>&quot;&gt;</span><br><span class="line">    &lt;<span class="symbol">a</span> <span class="symbol">href</span>=&quot;<span class="symbol">javascript:<span class="symbol">alert</span></span>(&#x27;[<span class="symbol">id</span>],[<span class="symbol">name</span>],[<span class="symbol">age</span>],[<span class="symbol">sex</span>]&#x27;)&quot;&gt;查看&lt;/<span class="symbol">a</span>&gt;</span><br><span class="line">    &lt;<span class="symbol">a</span> <span class="symbol">href</span>=&quot;<span class="symbol">javascript:<span class="symbol">alert</span></span>(&#x27;[<span class="symbol">id</span>],[<span class="symbol">name</span>],[<span class="symbol">age</span>],[<span class="symbol">sex</span>]&#x27;)&quot;&gt;修改&lt;/<span class="symbol">a</span>&gt;</span><br><span class="line">    &lt;<span class="symbol">a</span> <span class="symbol">href</span>=&quot;<span class="symbol">javascript:<span class="symbol">alert</span></span>(&#x27;[<span class="symbol">id</span>],[<span class="symbol">name</span>],[<span class="symbol">age</span>],[<span class="symbol">sex</span>]&#x27;)&quot;&gt;删除&lt;/<span class="symbol">a</span>&gt;</span><br><span class="line">  &lt;/<span class="symbol">div</span>&gt;</span><br><span class="line">&lt;/<span class="symbol">script</span>&gt;</span><br></pre></td></tr></table></figure><blockquote><p>调用模板</p></blockquote><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 给定需要插件执行的function：html</span><br><span class="line"><span class="variable">$</span>.template(<span class="string">&quot;html&quot;</span>, &#123;</span><br><span class="line">  template: <span class="variable">$</span>(<span class="string">&quot;#actionsTemp&quot;</span>),</span><br><span class="line">  <span class="keyword">data</span>: <span class="keyword">data</span>,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>]]></content>
    
    
    <summary type="html">&lt;h1 id=&quot;jquery-template&quot;&gt;&lt;a href=&quot;#jquery-template&quot; class=&quot;headerlink&quot; title=&quot;jquery-template&quot;&gt;&lt;/a&gt;jquery-template&lt;/h1&gt;&lt;h4 id=&quot;项目介绍&quot;&gt;&lt;a href=&quot;#项目介绍&quot; class=&quot;headerlink&quot; title=&quot;项目介绍&quot;&gt;&lt;/a&gt;项目介绍&lt;/h4&gt;&lt;p&gt;利用模板及正则解决js拼接html代码问题&lt;/p&gt;
&lt;p&gt;&lt;a target=&quot;_blank&quot; href=&quot;http://www.kiilin.com/jquery-template/&quot;&gt;演示地址&lt;/a&gt; | &lt;span class=&quot;exturl&quot; data-url=&quot;aHR0cHM6Ly9naXRlZS5jb20va2lpbGluL2pxdWVyeS10ZW1wbGF0ZQ==&quot;&gt;bug提交地址&lt;i class=&quot;fa fa-external-link-alt&quot;&gt;&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;</summary>
    
    
    
    <category term="JavaScript" scheme="https://www.kiilin.com/categories/JavaScript/"/>
    
    
    <category term="template" scheme="https://www.kiilin.com/tags/template/"/>
    
    <category term="模板" scheme="https://www.kiilin.com/tags/%E6%A8%A1%E6%9D%BF/"/>
    
    <category term="jquery" scheme="https://www.kiilin.com/tags/jquery/"/>
    
    <category term="正则" scheme="https://www.kiilin.com/tags/%E6%AD%A3%E5%88%99/"/>
    
  </entry>
  
  <entry>
    <title>CentOS下搭建Hexo + github 博客 多端同步 后台管理(未完成)</title>
    <link href="https://www.kiilin.com/blog/hexo_install.html"/>
    <id>https://www.kiilin.com/blog/hexo_install.html</id>
    <published>2018-03-01T11:44:00.000Z</published>
    <updated>2020-12-31T02:06:41.546Z</updated>
    
    <content type="html"><![CDATA[<p>很早就想搭建一个属于自己的博客，也试过博客园、CSDN的博客，但是总感觉不是真正属于自己的博客，而且看到广告很烦。偶然发现一篇Hexo+github搭建博客的教程，就开始了尝试。<br>本篇为从零搭建博客的教程，其他功能等待后续文章。</p><a id="more"></a><hr><h2 id="一、环境依赖"><a href="#一、环境依赖" class="headerlink" title="一、环境依赖"></a>一、环境依赖</h2><h5 id="首先你需要一台可以连接外网的linux主机（本文以阿里云ESC服务器为例，学生认证后购买有优惠），服务器相关配置相关内容不再赘述"><a href="#首先你需要一台可以连接外网的linux主机（本文以阿里云ESC服务器为例，学生认证后购买有优惠），服务器相关配置相关内容不再赘述" class="headerlink" title="首先你需要一台可以连接外网的linux主机（本文以阿里云ESC服务器为例，学生认证后购买有优惠），服务器相关配置相关内容不再赘述"></a>首先你需要一台可以连接外网的linux主机（本文以<span class="exturl" data-url="aHR0cHM6Ly93d3cuYWxpeXVuLmNvbS9wcm9kdWN0L2Vjcz9zcG09NTE3Ni44MTQyMDI5LjM4ODI2MS4yNDkuNWNlNzc2ZjQ2eTZjRHQ=">阿里云ESC服务器<i class="fa fa-external-link-alt"></i></span>为例，学生认证后购买有优惠），服务器相关配置相关内容不再赘述</h5><h5 id="服务器环境及版本"><a href="#服务器环境及版本" class="headerlink" title="服务器环境及版本"></a>服务器环境及版本</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">node····8.9.3</span><br><span class="line">git·····1.8.3.1</span><br><span class="line">hexo····3.6.0</span><br><span class="line">java····1.8.0_161 # 使用其他服务器中间件时不需要</span><br><span class="line">tomcat··9.0.5 # 使用其他服务器中间件时不需要</span><br><span class="line">hexo-admin # hexo博客管理工具</span><br></pre></td></tr></table></figure><hr><h2 id="二、环境配置"><a href="#二、环境配置" class="headerlink" title="二、环境配置"></a>二、环境配置</h2><p>本文所有安装路径在<code>/opt</code>目录结构下，所有目录结构仅供参考</p><h4 id="安装java，tomcat"><a href="#安装java，tomcat" class="headerlink" title="安装java，tomcat"></a>安装java，tomcat</h4><h5 id="也可用其他服务器中间件，在此不做说明"><a href="#也可用其他服务器中间件，在此不做说明" class="headerlink" title="也可用其他服务器中间件，在此不做说明"></a>也可用其他服务器中间件，在此不做说明</h5><h4 id="安装node-js"><a href="#安装node-js" class="headerlink" title="安装node.js"></a>安装node.js</h4><ul><li><h5 id="下载"><a href="#下载" class="headerlink" title="下载"></a>下载</h5>找到<span class="exturl" data-url="aHR0cDovL25vZGVqcy5jbi8=">最新的下载地址<i class="fa fa-external-link-alt"></i></span>，用wget命令下载到本地<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cd /opt</span><br><span class="line">mkdir node</span><br><span class="line">cd node</span><br><span class="line">wget http://cdn.npm.taobao.org/dist/node/v8.9.3/node-v8.9.3-linux-x86.tar.gz</span><br></pre></td></tr></table></figure></li><li><h5 id="解压文件"><a href="#解压文件" class="headerlink" title="解压文件"></a>解压文件</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf node-v8.9.3-linux-x86.tar.gz</span><br></pre></td></tr></table></figure></li><li><h5 id="配置环境变量"><a href="#配置环境变量" class="headerlink" title="配置环境变量"></a>配置环境变量</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 编辑 /etc/profile (使用vim)</span></span><br><span class="line">vim /etc/profile</span><br><span class="line"><span class="meta">#</span><span class="bash"> 按A插入编辑 vim编辑不在此赘述</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 在底部添加 PATH 变量</span></span><br><span class="line">export NODE_HOME=/opt/node/node-v8.9.3-linux-x64</span><br><span class="line">export PATH=$PATH:$NODE_HOME/bin</span><br><span class="line"><span class="meta">#</span><span class="bash"> 保存退出，先按esc键，再输入</span></span><br><span class="line">:wq!</span><br><span class="line"><span class="meta">#</span><span class="bash"> 最后保存并使其生效即可</span></span><br><span class="line">source /etc/profile</span><br></pre></td></tr></table></figure></li><li><h5 id="检查环境"><a href="#检查环境" class="headerlink" title="检查环境"></a>检查环境</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 检查node.js版本</span></span><br><span class="line">node -v</span><br></pre></td></tr></table></figure>  输出如下内容即表示安装成功  <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">v8.9.3 # 安装的版本号</span><br></pre></td></tr></table></figure><h4 id="安装git"><a href="#安装git" class="headerlink" title="安装git"></a>安装git</h4></li><li><h5 id="安装流程"><a href="#安装流程" class="headerlink" title="安装流程"></a>安装流程</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install git</span><br></pre></td></tr></table></figure></li><li><h5 id="检查环境-1"><a href="#检查环境-1" class="headerlink" title="检查环境"></a>检查环境</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 检查git.js版本</span></span><br><span class="line">git --version</span><br></pre></td></tr></table></figure>  输出如下内容即表示安装成功  <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git version 1.8.3.1 # 安装的版本号</span><br></pre></td></tr></table></figure></li></ul><h4 id="配置github-page"><a href="#配置github-page" class="headerlink" title="配置github page"></a>配置github page</h4><ul><li><h5 id="Github的优点"><a href="#Github的优点" class="headerlink" title="Github的优点"></a>Github的优点</h5><p>GitHub是基于git实现的代码托管。git可能是目前最好用的版本控制系统了，非常受欢迎。<br>GitHub可以免费使用，并且快速稳定。<br>Github上面的世界很精彩，用久了你的眼界会开阔很多。</p></li><li><h5 id="什么是Github-Pages"><a href="#什么是Github-Pages" class="headerlink" title="什么是Github Pages?"></a>什么是Github Pages?</h5><p>Github Pages可以被认为是用户编写的、托管在github上的静态网页。<span class="exturl" data-url="aHR0cHM6Ly9wYWdlcy5naXRodWIuY29tLw==">GitHub Pages<i class="fa fa-external-link-alt"></i></span> 本用于介绍托管在GitHub的项目，不过，由于他的空间免费稳定，用来做搭建一个博客再好不过了。<br><a href="https://pages.github.com/" title="github page首页"><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/image/github-page-index.jpg" alt="github page首页" title="github page首页"></a></p></li><li><h5 id="为什么要使用Github-Pages"><a href="#为什么要使用Github-Pages" class="headerlink" title="为什么要使用Github Pages"></a>为什么要使用Github Pages</h5><p>可以绑定你的域名(但暂时貌似只能绑定一个)。<br>简单快捷，使用Github Pages可以为你提供一个免费的服务器，免去了自己搭建服务器和写数据库的麻烦.</p></li><li><h5 id="注册GitHub账号"><a href="#注册GitHub账号" class="headerlink" title="注册GitHub账号"></a>注册GitHub账号</h5><p>在创建博客之前，当然必须有GitHub的帐号，该帐号将用来创建项目，默认的域名username.github.com/projectName中的username也要用到这个帐号。</p><blockquote><p>注意：下面涉及到的一些命令凡是更用户名和项目名有关的一律会用这里的username和projectName代替，注意替换<br>访问：<span class="exturl" data-url="aHR0cDovL3d3dy5naXRodWIuY29tLw==">http://www.github.com/<i class="fa fa-external-link-alt"></i></span><br>注册你的username和邮箱，邮箱十分重要，GitHub上很多通知都是通过邮箱的。比如你的主页上传并构建成功会通过邮箱通知，更重要的是，如果构建失败的话也会在邮件中说明原因。</p></blockquote></li><li><h5 id="创建项目仓库"><a href="#创建项目仓库" class="headerlink" title="创建项目仓库"></a>创建项目仓库</h5><p>在创建博客之前，还需要用已有的帐号创建一个项目，上面那个链接的projectName将是这里即将创建的项目名称。在Git中，项目被称为仓库(Repository)，仓库顾名思义，当然可以包含代码或者非代码。将来我们的网页或者模板实际上都是保存在这个仓库中的。</p></li></ul><ol><li>登录后，点击头像旁边+号选择<code>new repository</code>创建仓库，如下图<br><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/image/new%20repository.jpg" alt="新建github仓库入口" title="新建github仓库入口"></li><li>填写仓库信息，如下图<br><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/image/new%20repository%20info.jpg" alt="填写github page信息" title="填写github page信息"><blockquote><ol><li>每个帐号只能有一个仓库来存放个人主页，而且仓库的名字必须是<code>username/username.github.io</code>这是特殊的命名约定。你可以通过<span class="exturl" data-url="aHR0cHM6Ly91c2VybmFtZS5naXRodWIuaW8v">https://username.github.io<i class="fa fa-external-link-alt"></i></span> 来访问你的个人主页。</li><li>上图<code>Repository name</code>必须为<code>username.github.io</code>其中<code>username</code>为你的用户名，和前面<code>Owner</code>相同；<code>Description</code> 为仓库描述，根据需要填写</li><li>创建了仓库后，我们就需要管理它，无论是管理本地仓库还是远程仓库都需要Git客户端；Git客户端实际上十分强大，它本身就可以offline的创建本地仓库，而本地仓库和远程仓库之间的同步也是通过Git客户端完成的。</li></ol></blockquote></li></ol><ul><li><h5 id="SSH配置"><a href="#SSH配置" class="headerlink" title="SSH配置"></a>SSH配置</h5>上传文件需要配置ssh key，不然无法上传。</li></ul><ol><li>首先先检查一下本地是否已经存在ssh key,在Git Bash输入以下指令（任意位置点击鼠标右键），检查是否已经存在了SSH keys。<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ls -al ~/.ssh</span><br></pre></td></tr></table></figure></li><li>如果不存在就没有关系，如果存在的话，在保证没有其他人使用的情况下可以直接删除<code>.ssh</code>文件夹里面所有文件：<br>如果存在将显示为：<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">total 20</span><br><span class="line">drwx------  2 root root 4096 Mar  1 17:05 .</span><br><span class="line">dr-xr-x---. 8 root root 4096 Mar  2 10:28 ..</span><br><span class="line">-rw-------  1 root root    0 Mar  1 14:11 authorized_keys</span><br><span class="line">-rw-------  1 root root 1679 Mar  1 16:55 id_rsa</span><br><span class="line">-rw-r--r--  1 root root  397 Mar  1 16:55 id_rsa.pub</span><br><span class="line">-rw-r--r--  1 root root 1197 Mar  1 17:12 known_hosts</span><br></pre></td></tr></table></figure></li><li>设置name和emai<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.email &quot;you@example.com&quot;</span><br><span class="line">git config --global user.name &quot;Your Name&quot;</span><br></pre></td></tr></table></figure>需要注意的是这里的name是随意的，邮箱是你的联系邮箱，与github上的邮箱没有什么联系（不过我都是同一个邮箱）。</li><li>生成ssh 密钥<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh-keygen -t rsa -C &quot;username@domain.com&quot; # 邮箱就是你注册Github时候的邮箱</span><br></pre></td></tr></table></figure>按三次回车键即可，如果设置了密码请记住。<br>这一步在~/.ssh/下生成了两个文件id_rsa 和 id_rsa.pub</li><li>获取Key<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cat ~/.ssh/id_rsa.pub</span><br></pre></td></tr></table></figure>然后复制key</li><li>在Github上添加SSH密钥</li><li>点击头像选择下方Setting进入<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3NldHRpbmdzL3Byb2ZpbGU=">设置界面<i class="fa fa-external-link-alt"></i></span>，如下图<br><a href="https://github.com/settings/profile" title="设置界面入口"><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/image/setting.jpg" alt="设置界面入口" title="设置界面入口"></a><br>在左侧选择<code>SSH and GPG keys</code>，如下图<br><a href="https://github.com/settings/keys" title="SSH key 入口"><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/image/SSH.jpg" alt="SSH key 入口" title="SSH key 入口"></a><br>我这里已经填写了两组key，选择上方<code>New SSH key</code>新建 SSH key<br><a href="https://github.com/settings/keys" title="SSH key 管理"><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/image/key.jpg" alt="SSH key 管理" title="SSH key 管理"></a><br>粘贴刚刚复制的key到下方输入框，<code>Title</code>为key的自定义名称<br><a href="https://github.com/settings/ssh/new" title="新建SSH key"><img src="http://wagk.oss-cn-huhehaote.aliyuncs.com/hexo/image/new%20key.jpg" alt="新建SSH key" title="新建SSH key"></a><br>之后保存完成</li></ol><h4 id="安装hexo"><a href="#安装hexo" class="headerlink" title="安装hexo"></a>安装hexo</h4><ul><li><h5 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">cd /opt</span><br><span class="line"><span class="meta">#</span><span class="bash"> 创建目录</span></span><br><span class="line">mkdir blog</span><br><span class="line"><span class="meta">#</span><span class="bash"> 安装 Hexo</span></span><br><span class="line">npm install -g hexo</span><br><span class="line"><span class="meta">#</span><span class="bash"> 初始化 Hexo</span></span><br><span class="line">hexo init blog</span><br></pre></td></tr></table></figure></li><li><h5 id="安装插件"><a href="#安装插件" class="headerlink" title="安装插件"></a>安装插件</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 安装插件</span></span><br><span class="line">npm install</span><br><span class="line"><span class="meta">#</span><span class="bash"> 如果使用过程中确实某些插件的，可根据以下插件检查</span></span><br><span class="line">npm install hexo-generator-index --save</span><br><span class="line">npm install hexo-generator-archive --save</span><br><span class="line">npm install hexo-generator-category --save</span><br><span class="line">npm install hexo-generator-tag --save</span><br><span class="line">npm install hexo-server --save</span><br><span class="line">npm install hexo-deployer-git --save</span><br><span class="line">npm install hexo-deployer-heroku --save</span><br><span class="line">npm install hexo-deployer-rsync --save</span><br><span class="line">npm install hexo-deployer-openshift --save</span><br><span class="line">npm install hexo-renderer-marked --save</span><br><span class="line">npm install hexo-renderer-stylus --save</span><br><span class="line">npm install hexo-generator-feed --save</span><br><span class="line">npm install hexo-generator-sitemap --save</span><br></pre></td></tr></table></figure></li><li><h5 id="修改Hexo配置文件"><a href="#修改Hexo配置文件" class="headerlink" title="修改Hexo配置文件"></a>修改Hexo配置文件</h5><blockquote><p>配置文件路径： <code>./_config.yml</code> 以下为我的配置，仅供参考<br>提示：key对应没有值的时候，冒号后面一定要有空格！否则会报错<br>如: timezone:会报错，timezone: 则不会。</p></blockquote></li><li><h5 id="Hexo配置文件参考"><a href="#Hexo配置文件参考" class="headerlink" title="Hexo配置文件参考"></a>Hexo配置文件参考</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> Hexo Configuration Hexo配置文件</span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment"># Docs: https://hexo.io/docs/configuration.html</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment"># Source: https://github.com/hexojs/hexo/</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 网站信息</span></span><br><span class="line"><span class="meta">#</span><span class="bash">标题</span></span><br><span class="line">title: 某某の博客</span><br><span class="line"><span class="meta">#</span><span class="bash">副标题</span></span><br><span class="line">subtitle: 博客的副标题</span><br><span class="line"><span class="meta">#</span><span class="bash">网站描述</span></span><br><span class="line">description: 博客的描述</span><br><span class="line"><span class="meta">#</span><span class="bash">作者昵称</span></span><br><span class="line">author: 作者</span><br><span class="line"><span class="meta">#</span><span class="bash">网站语言，默认英语，设置简体汉语</span></span><br><span class="line">language: zh-CN</span><br><span class="line"><span class="meta">#</span><span class="bash">时区，默认电脑时区</span></span><br><span class="line">timezone: Asia/Shanghai</span><br><span class="line"><span class="meta">#</span><span class="bash"> 网址设置</span></span><br><span class="line"><span class="meta">#</span><span class="bash">如果网站是放在子目录中，将url设置成<span class="string">&#x27;http://yoursite.com/child&#x27;</span>，将root设置成<span class="string">&#x27;/child/&#x27;</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment"># If your site is put in a subdirectory, set url as &#x27;http://yoursite.com/child&#x27; and root as &#x27;/child/&#x27;</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash">网址</span></span><br><span class="line">url: http://www.domain.com</span><br><span class="line"><span class="meta">#</span><span class="bash">网站根目录。如果网站是放在子目录中，将root设置成<span class="string">&#x27;子目录名&#x27;</span></span></span><br><span class="line">root: /</span><br><span class="line"><span class="meta">#</span><span class="bash">文章链接地址格式 。即文章存放的目录。</span></span><br><span class="line">permalink: :year/:month/:day/:title/</span><br><span class="line">permalink_defaults:</span><br><span class="line"><span class="meta">#</span><span class="bash"> 目录设置</span></span><br><span class="line"><span class="meta">#</span><span class="bash">资源文件夹，放在里面的文件会上传到github中</span></span><br><span class="line">source_dir: source</span><br><span class="line"><span class="meta">#</span><span class="bash">公共文件夹，存放生成的静态文件</span></span><br><span class="line">public_dir: public</span><br><span class="line"><span class="meta">#</span><span class="bash">标签文件夹，默认是tags。实际存放在<span class="built_in">source</span>/tags中。</span></span><br><span class="line">tag_dir: tags</span><br><span class="line">rss_dir: rss</span><br><span class="line"><span class="meta">#</span><span class="bash">档案文件夹，默认是archives。</span></span><br><span class="line">archive_dir: archives</span><br><span class="line"><span class="meta">#</span><span class="bash">分类文件夹，默认是categories。实际存放在<span class="built_in">source</span>/categories中。</span></span><br><span class="line">category_dir: categories</span><br><span class="line"><span class="meta">#</span><span class="bash">代码文件夹，默认是downloads/code</span></span><br><span class="line">code_dir: downloads/code</span><br><span class="line"><span class="meta">#</span><span class="bash">国际化文件夹，默认跟language相同</span></span><br><span class="line">i18n_dir: :lang</span><br><span class="line"><span class="meta">#</span><span class="bash">不需要渲染的文件夹或文件夹,放在[]中</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 这两个文件是百度和google的站长验证文件，不能渲染，否则会改变内容，不能验证过</span></span><br><span class="line">skip_render: [baidu_verify_R9MZjdMkXT.html, google0f8fac7da2b48ef8.html, README.md, 模板.md]</span><br><span class="line"><span class="meta">#</span><span class="bash"> 写作选项</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 新建博文（帖子）的默认名称</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> File name of new posts</span></span><br><span class="line">new_post_name: :title.md</span><br><span class="line"><span class="meta">#</span><span class="bash">默认布局模板是post，而不是draft和page</span></span><br><span class="line">default_layout: post</span><br><span class="line"><span class="meta">#</span><span class="bash">是否将标题转换成标题形式（首字母大写）</span></span><br><span class="line">titlecase: false # Transform title into titlecase</span><br><span class="line"><span class="meta">#</span><span class="bash">在新标签页面中打开网页</span></span><br><span class="line">external_link: true # Open external links in new tab</span><br><span class="line">filename_case: 0</span><br><span class="line"><span class="meta">#</span><span class="bash">是否渲染草稿</span></span><br><span class="line">render_drafts: false</span><br><span class="line"><span class="meta">#</span><span class="bash">启动 Asset 文件夹</span></span><br><span class="line">post_asset_folder: false</span><br><span class="line"><span class="meta">#</span><span class="bash">把链接改为与根目录的相对位址</span></span><br><span class="line">relative_link: false</span><br><span class="line"><span class="meta">#</span><span class="bash">显示未来的文章</span></span><br><span class="line">future: true</span><br><span class="line"><span class="meta">#</span><span class="bash">代码块的设置</span></span><br><span class="line">highlight:</span><br><span class="line">  enable: true          #  使用代码高亮</span><br><span class="line">  line_number: true # 显示行号</span><br><span class="line">  auto_detect: true  # 自动检测语言</span><br><span class="line">  tab_replace:</span><br><span class="line"><span class="meta">#</span><span class="bash"> 分类和标签</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 默认分类</span></span><br><span class="line">default_category: uncategorized</span><br><span class="line"><span class="meta">#</span><span class="bash">分类别名</span></span><br><span class="line">category_map:</span><br><span class="line"><span class="meta">#</span><span class="bash">标签别名</span></span><br><span class="line">tag_map:</span><br><span class="line"><span class="meta">#</span><span class="bash"> 日期和时间格式</span></span><br><span class="line"><span class="meta">#</span><span class="bash">Hexo 使用 Moment.js 来解析和显示时间。</span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment"># You can customize the date format as defined in</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment"># http://momentjs.com/docs/#/displaying/format/</span></span></span><br><span class="line">date_format: YYYY-MM-DD</span><br><span class="line">time_format: HH:mm:ss</span><br><span class="line"><span class="meta">#</span><span class="bash"> 分页配置</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> ---------------下面选项需要对应插件的支持---------------</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> npm install hexo-generator-index --save</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> npm install hexo-generator-archive --save</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> npm install hexo-generator-category --save</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> npm install hexo-generator-tag --save</span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment"># Set per_page to 0 to disable pagination</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash">每页显示的文章量</span></span><br><span class="line"><span class="meta">#</span><span class="bash">per_page: 20</span></span><br><span class="line"><span class="meta">#</span><span class="bash">首页的分页设置</span></span><br><span class="line">index_generator:</span><br><span class="line">  per_page: 5</span><br><span class="line"><span class="meta">#</span><span class="bash">归档页的分页设置</span></span><br><span class="line">archive_generator:</span><br><span class="line">  per_page: 30</span><br><span class="line">  yearly: true</span><br><span class="line">  monthly: true</span><br><span class="line"><span class="meta">#</span><span class="bash">标签页的分页设置</span></span><br><span class="line">tag_generator:</span><br><span class="line">  per_page: 20</span><br><span class="line"><span class="meta">#</span><span class="bash">分页路径，在public中可以看到</span></span><br><span class="line"><span class="meta">#</span><span class="bash">pagination_dir: page</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> Extensions 拓展插件配置</span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment"># Plugins: https://hexo.io/plugins/</span></span></span><br><span class="line">plugins:</span><br><span class="line">baidusitemap:</span><br><span class="line">  path: baidusitemap.xml</span><br><span class="line"><span class="meta">#</span><span class="bash"> 配置RSS</span></span><br><span class="line">feed:</span><br><span class="line"><span class="meta">  #</span><span class="bash">feed 类型 (atom/rss2)</span></span><br><span class="line">  type: atom</span><br><span class="line"><span class="meta">  #</span><span class="bash">rss 路径</span></span><br><span class="line">  path: atom.xm</span><br><span class="line"><span class="meta">  #</span><span class="bash">在 rss 中最多生成的文章数(0显示所有)</span></span><br><span class="line">  limit: 0</span><br><span class="line"><span class="meta">#</span><span class="bash"> 自定义站点内容搜索</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 需要先安装插件：</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> npm install hexo-generator-search --save</span></span><br><span class="line">search:</span><br><span class="line">  path: search.xml</span><br><span class="line"><span class="meta">  #</span><span class="bash"> 如只想索引文章，可设置为post</span></span><br><span class="line">  field: all</span><br><span class="line"><span class="meta">#</span><span class="bash"> 主题配置</span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment"># Themes: https://hexo.io/themes/</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash">theme: <span class="literal">false</span> <span class="comment">#禁用主题</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash">theme: landscape</span></span><br><span class="line">theme: next</span><br><span class="line"><span class="meta">#</span><span class="bash"> 部署配置</span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment"># Docs: https://hexo.io/docs/deployment.html</span></span></span><br><span class="line">deploy:</span><br><span class="line">  type: git</span><br><span class="line">  repository: git@github.com:name/name.github.io.git # 该配置后续说明</span><br><span class="line">  branch: master</span><br></pre></td></tr></table></figure><p><b style="color: red; font-size: 20px;">1111</b></p></li></ul><p>本文参考：<br>1.<span class="exturl" data-url="aHR0cHM6Ly93d3cuamlhbnNodS5jb20vcC8wODIzZTM4N2MwMTk=">https://www.jianshu.com/p/0823e387c019<i class="fa fa-external-link-alt"></i></span></p>]]></content>
    
    
    <summary type="html">&lt;p&gt;很早就想搭建一个属于自己的博客，也试过博客园、CSDN的博客，但是总感觉不是真正属于自己的博客，而且看到广告很烦。偶然发现一篇Hexo+github搭建博客的教程，就开始了尝试。&lt;br&gt;本篇为从零搭建博客的教程，其他功能等待后续文章。&lt;/p&gt;</summary>
    
    
    
    <category term="安装" scheme="https://www.kiilin.com/categories/%E5%AE%89%E8%A3%85/"/>
    
    
    <category term="centos" scheme="https://www.kiilin.com/tags/centos/"/>
    
    <category term="hexo" scheme="https://www.kiilin.com/tags/hexo/"/>
    
    <category term="github" scheme="https://www.kiilin.com/tags/github/"/>
    
    <category term="hexo-admin" scheme="https://www.kiilin.com/tags/hexo-admin/"/>
    
  </entry>
  
</feed>
